getTranslationLanguages()) as $langcode) { $translation = $this->getTranslation($langcode); // If no owner has been set explicitly, make the anonymous user the owner. if (!$translation->getOwner()) { $translation->setOwnerId(0); } } // If no revision author has been set explicitly, make the node owner the // revision author. if (!$this->getRevisionUser()) { $this->setRevisionUserId($this->getOwnerId()); } } /** * {@inheritdoc} */ public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) { parent::preSaveRevision($storage, $record); if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) { // If we are updating an existing node without adding a new revision, we // need to make sure $entity->revision_log is reset whenever it is empty. // Therefore, this code allows us to avoid clobbering an existing log // entry with an empty one. $record->revision_log = $this->original->revision_log->value; } } /** * {@inheritdoc} */ public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); // Update the node access table for this node, but only if it is the // default revision. There's no need to delete existing records if the node // is new. if ($this->isDefaultRevision()) { /** @var \Drupal\node\NodeAccessControlHandlerInterface $access_control_handler */ $access_control_handler = \Drupal::entityTypeManager()->getAccessControlHandler('node'); $grants = $access_control_handler->acquireGrants($this); \Drupal::service('node.grant_storage')->write($this, $grants, NULL, $update); } // Reindex the node when it is updated. The node is automatically indexed // when it is added, simply by being added to the node table. if ($update) { node_reindex_node_search($this->id()); } } /** * {@inheritdoc} */ public static function preDelete(EntityStorageInterface $storage, array $entities) { parent::preDelete($storage, $entities); // Ensure that all nodes deleted are removed from the search index. if (\Drupal::hasService('search.index')) { /** @var \Drupal\search\SearchIndexInterface $search_index */ $search_index = \Drupal::service('search.index'); foreach ($entities as $entity) { $search_index->clear('node_search', $entity->nid->value); } } } /** * {@inheritdoc} */ public static function postDelete(EntityStorageInterface $storage, array $nodes) { parent::postDelete($storage, $nodes); \Drupal::service('node.grant_storage')->deleteNodeRecords(array_keys($nodes)); } /** * {@inheritdoc} */ public function getType() { return $this->bundle(); } /** * {@inheritdoc} */ public function access($operation = 'view', AccountInterface $account = NULL, $return_as_object = FALSE) { // This override exists to set the operation to the default value "view". return parent::access($operation, $account, $return_as_object); } /** * {@inheritdoc} */ public function getTitle() { return $this->get('title')->value; } /** * {@inheritdoc} */ public function setTitle($title) { $this->set('title', $title); return $this; } /** * {@inheritdoc} */ public function getCreatedTime() { return $this->get('created')->value; } /** * {@inheritdoc} */ public function setCreatedTime($timestamp) { $this->set('created', $timestamp); return $this; } /** * {@inheritdoc} */ public function isPromoted() { return (bool) $this->get('promote')->value; } /** * {@inheritdoc} */ public function setPromoted($promoted) { $this->set('promote', $promoted ? NodeInterface::PROMOTED : NodeInterface::NOT_PROMOTED); return $this; } /** * {@inheritdoc} */ public function isSticky() { return (bool) $this->get('sticky')->value; } /** * {@inheritdoc} */ public function setSticky($sticky) { $this->set('sticky', $sticky ? NodeInterface::STICKY : NodeInterface::NOT_STICKY); return $this; } /** * {@inheritdoc} */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields = parent::baseFieldDefinitions($entity_type); $fields += static::ownerBaseFieldDefinitions($entity_type); $fields['title'] = BaseFieldDefinition::create('string') ->setLabel(t('Title')) ->setRequired(TRUE) ->setTranslatable(TRUE) ->setRevisionable(TRUE) ->setSetting('max_length', 255) ->setDisplayOptions('view', [ 'label' => 'hidden', 'type' => 'string', 'weight' => -5, ]) ->setDisplayOptions('form', [ 'type' => 'string_textfield', 'weight' => -5, ]) ->setDisplayConfigurable('form', TRUE); $fields['uid'] ->setLabel(t('Authored by')) ->setDescription(t('The username of the content author.')) ->setRevisionable(TRUE) ->setDisplayOptions('view', [ 'label' => 'hidden', 'type' => 'author', 'weight' => 0, ]) ->setDisplayOptions('form', [ 'type' => 'entity_reference_autocomplete', 'weight' => 5, 'settings' => [ 'match_operator' => 'CONTAINS', 'size' => '60', 'placeholder' => '', ], ]) ->setDisplayConfigurable('form', TRUE); $fields['status'] ->setDisplayOptions('form', [ 'type' => 'boolean_checkbox', 'settings' => [ 'display_label' => TRUE, ], 'weight' => 120, ]) ->setDisplayConfigurable('form', TRUE); $fields['created'] = BaseFieldDefinition::create('created') ->setLabel(t('Authored on')) ->setDescription(t('The date and time that the content was created.')) ->setRevisionable(TRUE) ->setTranslatable(TRUE) ->setDisplayOptions('view', [ 'label' => 'hidden', 'type' => 'timestamp', 'weight' => 0, ]) ->setDisplayOptions('form', [ 'type' => 'datetime_timestamp', 'weight' => 10, ]) ->setDisplayConfigurable('form', TRUE); $fields['changed'] = BaseFieldDefinition::create('changed') ->setLabel(t('Changed')) ->setDescription(t('The time that the node was last edited.')) ->setRevisionable(TRUE) ->setTranslatable(TRUE); $fields['promote'] = BaseFieldDefinition::create('boolean') ->setLabel(t('Promoted to front page')) ->setRevisionable(TRUE) ->setTranslatable(TRUE) ->setDefaultValue(TRUE) ->setDisplayOptions('form', [ 'type' => 'boolean_checkbox', 'settings' => [ 'display_label' => TRUE, ], 'weight' => 15, ]) ->setDisplayConfigurable('form', TRUE); $fields['sticky'] = BaseFieldDefinition::create('boolean') ->setLabel(t('Sticky at top of lists')) ->setRevisionable(TRUE) ->setTranslatable(TRUE) ->setDefaultValue(FALSE) ->setDisplayOptions('form', [ 'type' => 'boolean_checkbox', 'settings' => [ 'display_label' => TRUE, ], 'weight' => 16, ]) ->setDisplayConfigurable('form', TRUE); return $fields; } }