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