/** * Checks access to the node preview page. * * @param \Drupal\Core\Session\AccountInterface $account * The currently logged in account. * @param \Drupal\node\NodeInterface $node_preview * The node that is being previewed. * * @return string * A \Drupal\Core\Access\AccessInterface constant value. */ public function access(AccountInterface $account, NodeInterface $node_preview) { if ($node_preview->isNew()) { $access_controller = $this->entityManager->getAccessControlHandler('node'); return $access_controller->createAccess($node_preview->bundle(), $account, [], TRUE); } else { return $node_preview->access('update', $account, TRUE); } }
/** * {@inheritdoc} */ public function addFormElements(array $form, FormStateInterface $form_state, NodeInterface $node, AccountInterface $account, $collapsed = TRUE) { // If the form is being processed during the Ajax callback of our book bid // dropdown, then $form_state will hold the value that was selected. if ($form_state->hasValue('book')) { $node->book = $form_state->getValue('book'); } $form['book'] = array('#type' => 'details', '#title' => $this->t('Book outline'), '#weight' => 10, '#open' => !$collapsed, '#group' => 'advanced', '#attributes' => array('class' => array('book-outline-form')), '#attached' => array('library' => array('book/drupal.book')), '#tree' => TRUE); foreach (array('nid', 'has_children', 'original_bid', 'parent_depth_limit') as $key) { $form['book'][$key] = array('#type' => 'value', '#value' => $node->book[$key]); } $form['book']['pid'] = $this->addParentSelectFormElements($node->book); // @see \Drupal\book\Form\BookAdminEditForm::bookAdminTableTree(). The // weight may be larger than 15. $form['book']['weight'] = array('#type' => 'weight', '#title' => $this->t('Weight'), '#default_value' => $node->book['weight'], '#delta' => max(15, abs($node->book['weight'])), '#weight' => 5, '#description' => $this->t('Pages at a given level are ordered first by weight and then by title.')); $options = array(); $nid = !$node->isNew() ? $node->id() : 'new'; if ($node->id() && $nid == $node->book['original_bid'] && $node->book['parent_depth_limit'] == 0) { // This is the top level node in a maximum depth book and thus cannot be // moved. $options[$node->id()] = $node->label(); } else { foreach ($this->getAllBooks() as $book) { $options[$book['nid']] = $book['title']; } } if ($account->hasPermission('create new books') && ($nid == 'new' || $nid != $node->book['original_bid'])) { // The node can become a new book, if it is not one already. $options = array($nid => $this->t('- Create a new book -')) + $options; } if (!$node->book['bid']) { // The node is not currently in the hierarchy. $options = array(0 => $this->t('- None -')) + $options; } // Add a drop-down to select the destination book. $form['book']['bid'] = array('#type' => 'select', '#title' => $this->t('Book'), '#default_value' => $node->book['bid'], '#options' => $options, '#access' => (bool) $options, '#description' => $this->t('Your page will be a part of the selected book.'), '#weight' => -5, '#attributes' => array('class' => array('book-title-select')), '#ajax' => array('callback' => 'book_form_update', 'wrapper' => 'edit-book-plid-wrapper', 'effect' => 'fade', 'speed' => 'fast')); return $form; }
/** * Retrieve the initial state value of the entity. * * The state_machine module uses a protected property called initialValue to * get the initial state which is inaccessible. During an entity update, the * typedDataManager attempts to validate the field but the constraint again * calls for the Guard to check the allowed states. * The issue is that the entity object already carries the new value * at this point, so it attempts to check a to_state to to_state transition. * In order to check the initial value, we get the unchanged version of the * object from the database. * * @param \Drupal\node\NodeInterface $entity * The node entity. * * @return string * The machine name value of the state. * * @see https://www.drupal.org/node/2745673 */ protected function getState(NodeInterface $entity) { if ($entity->isNew()) { return $entity->field_news_state->first()->value; } else { $unchanged_entity = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($entity->id()); return $unchanged_entity->field_news_state->first()->value; } }
/** * {@inheritdoc} */ public function createIndex(NodeInterface $node) { $query = $this->database->insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp')); foreach ($node->getTranslationLanguages() as $langcode => $language) { $translation = $node->getTranslation($langcode); foreach ($translation->taxonomy_forums as $item) { $query->values(array('nid' => $node->id(), 'title' => $translation->label(), 'tid' => $item->target_id, 'sticky' => (int) $node->isSticky(), 'created' => $node->getCreatedTime(), 'comment_count' => 0, 'last_comment_timestamp' => $node->getCreatedTime())); } } $query->execute(); // The logic for determining last_comment_count is fairly complex, so // update the index too. if ($node->isNew()) { $this->updateIndex($node); } }