/** * Override the behavior of title(). Get the title of the node. */ public function titleQuery() { $titles = array(); $nodes = $this->nodeStorage->loadMultiple($this->value); foreach ($nodes as $node) { $titles[] = SafeMarkup::checkPlain($node->label()); } return $titles; }
public function preRender($values) { $nids = array(); foreach ($values as $row) { $nids[] = $row->{$this->field_alias}; } if (!empty($nids)) { $this->nodes = $this->nodeStorage->loadMultiple($nids); } }
/** * {@inheritdoc} */ public function getArgument() { // Use the argument_default_node plugin to get the nid argument. $nid = parent::getArgument(); if (!empty($nid)) { $node = $this->nodeStorage->load($nid); if (isset($node->book['bid'])) { return $node->book['bid']; } } }
/** * Override the behavior of title(). Get the title of the revision. */ public function titleQuery() { $titles = array(); $results = $this->database->query('SELECT nr.vid, nr.nid, npr.title FROM {node_revision} nr WHERE nr.vid IN ( :vids[] )', array(':vids[]' => $this->value))->fetchAllAssoc('vid', PDO::FETCH_ASSOC); $nids = array(); foreach ($results as $result) { $nids[] = $result['nid']; } $nodes = $this->nodeStorage->loadMultiple(array_unique($nids)); foreach ($results as $result) { $nodes[$result['nid']]->set('title', $result['title']); $titles[] = String::checkPlain($nodes[$result['nid']]->label()); } return $titles; }
/** * Checks that the "authored by" works correctly with various values. * * @param \Drupal\node\NodeInterface $node * A node object. * @param string $form_element_name * The name of the form element to populate. */ protected function checkVariousAuthoredByValues(NodeInterface $node, $form_element_name) { // Try to change the 'authored by' field to an invalid user name. $edit = array($form_element_name => 'invalid-name'); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published')); $this->assertRaw(t('There are no entities matching "%name".', array('%name' => 'invalid-name'))); // Change the authored by field to an empty string, which should assign // authorship to the anonymous user (uid 0). $edit[$form_element_name] = ''; $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published')); $this->nodeStorage->resetCache(array($node->id())); $node = $this->nodeStorage->load($node->id()); $uid = $node->getOwnerId(); // Most SQL database drivers stringify fetches but entities are not // necessarily stored in a SQL database. At the same time, NULL/FALSE/"" // won't do. $this->assertTrue($uid === 0 || $uid === '0', 'Node authored by anonymous user.'); // Change the authored by field to another user's name (that is not // logged in). $edit[$form_element_name] = $this->webUser->getUsername(); $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published')); $this->nodeStorage->resetCache(array($node->id())); $node = $this->nodeStorage->load($node->id()); $this->assertIdentical($node->getOwnerId(), $this->webUser->id(), 'Node authored by normal user.'); }
/** * Checks node revision access. * * @param \Drupal\node\NodeInterface $node * The node to check. * @param \Drupal\Core\Session\AccountInterface $account * A user object representing the user for whom the operation is to be * performed. * @param string $op * (optional) The specific operation being checked. Defaults to 'view.' * @param string|null $langcode * (optional) Language code for the variant of the node. Different language * variants might have different permissions associated. If NULL, the * original langcode of the node is used. Defaults to NULL. * * @return bool * TRUE if the operation may be performed, FALSE otherwise. */ public function checkAccess(NodeInterface $node, AccountInterface $account, $op = 'view', $langcode = NULL) { $map = array('view' => 'view all revisions', 'update' => 'revert all revisions', 'delete' => 'delete all revisions'); $bundle = $node->bundle(); $type_map = array('view' => "view {$bundle} revisions", 'update' => "revert {$bundle} revisions", 'delete' => "delete {$bundle} revisions"); if (!$node || !isset($map[$op]) || !isset($type_map[$op])) { // If there was no node to check against, or the $op was not one of the // supported ones, we return access denied. return FALSE; } // If no language code was provided, default to the node revision's langcode. if (empty($langcode)) { $langcode = $node->language()->getId(); } // Statically cache access by revision ID, language code, user account ID, // and operation. $cid = $node->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $op; if (!isset($this->access[$cid])) { // Perform basic permission checks first. if (!$account->hasPermission($map[$op]) && !$account->hasPermission($type_map[$op]) && !$account->hasPermission('administer nodes')) { $this->access[$cid] = FALSE; return FALSE; } // There should be at least two revisions. If the vid of the given node // and the vid of the default revision differ, then we already have two // different revisions so there is no need for a separate database check. // Also, if you try to revert to or delete the default revision, that's // not good. if ($node->isDefaultRevision() && ($this->nodeStorage->countDefaultLanguageRevisions($node) == 1 || $op == 'update' || $op == 'delete')) { $this->access[$cid] = FALSE; } elseif ($account->hasPermission('administer nodes')) { $this->access[$cid] = TRUE; } else { // First check the access to the default revision and finally, if the // node passed in is not the default revision then access to that, too. $this->access[$cid] = $this->nodeAccess->access($this->nodeStorage->load($node->id()), $op, $langcode, $account) && ($node->isDefaultRevision() || $this->nodeAccess->access($node, $op, $langcode, $account)); } } return $this->access[$cid]; }
/** * Gets a list of node revision IDs for a specific node. * * @param \Drupal\node\NodeInterface * The node entity. * @param \Drupal\node\NodeStorageInterface $node_storage * The node storage handler. * * @return int[] * Node revision IDs (in descending order). */ protected function _getRevisionIds(NodeInterface $node, NodeStorageInterface $node_storage) { $result = $node_storage->getQuery()->allRevisions()->condition($node->getEntityType()->getKey('id'), $node->id())->sort($node->getEntityType()->getKey('revision'), 'DESC')->pager(50)->execute(); return array_keys($result); }
/** * Retrieves the last created node. */ public function getLastNode() { $nids = $this->entityQuery->get('node')->sort('created', 'DESC')->range(0, 1)->execute(); $nid = reset($nids); return $nid ? $this->nodeStorage->load($nid) : FALSE; }