node.api.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. /**
  3. * @file
  4. * Hooks specific to the Node module.
  5. */
  6. use Drupal\node\NodeInterface;
  7. use Drupal\Component\Utility\Html;
  8. use Drupal\Component\Utility\Xss;
  9. use Drupal\Core\Access\AccessResult;
  10. /**
  11. * @addtogroup hooks
  12. * @{
  13. */
  14. /**
  15. * Inform the node access system what permissions the user has.
  16. *
  17. * This hook is for implementation by node access modules. In this hook,
  18. * the module grants a user different "grant IDs" within one or more
  19. * "realms". In hook_node_access_records(), the realms and grant IDs are
  20. * associated with permission to view, edit, and delete individual nodes.
  21. *
  22. * The realms and grant IDs can be arbitrarily defined by your node access
  23. * module; it is common to use role IDs as grant IDs, but that is not required.
  24. * Your module could instead maintain its own list of users, where each list has
  25. * an ID. In that case, the return value of this hook would be an array of the
  26. * list IDs that this user is a member of.
  27. *
  28. * A node access module may implement as many realms as necessary to properly
  29. * define the access privileges for the nodes. Note that the system makes no
  30. * distinction between published and unpublished nodes. It is the module's
  31. * responsibility to provide appropriate realms to limit access to unpublished
  32. * content.
  33. *
  34. * Node access records are stored in the {node_access} table and define which
  35. * grants are required to access a node. There is a special case for the view
  36. * operation -- a record with node ID 0 corresponds to a "view all" grant for
  37. * the realm and grant ID of that record. If there are no node access modules
  38. * enabled, the core node module adds a node ID 0 record for realm 'all'. Node
  39. * access modules can also grant "view all" permission on their custom realms;
  40. * for example, a module could create a record in {node_access} with:
  41. * @code
  42. * $record = array(
  43. * 'nid' => 0,
  44. * 'gid' => 888,
  45. * 'realm' => 'example_realm',
  46. * 'grant_view' => 1,
  47. * 'grant_update' => 0,
  48. * 'grant_delete' => 0,
  49. * );
  50. * db_insert('node_access')->fields($record)->execute();
  51. * @endcode
  52. * And then in its hook_node_grants() implementation, it would need to return:
  53. * @code
  54. * if ($op == 'view') {
  55. * $grants['example_realm'] = array(888);
  56. * }
  57. * @endcode
  58. * If you decide to do this, be aware that the node_access_rebuild() function
  59. * will erase any node ID 0 entry when it is called, so you will need to make
  60. * sure to restore your {node_access} record after node_access_rebuild() is
  61. * called.
  62. *
  63. * For a detailed example, see node_access_example.module.
  64. *
  65. * @param \Drupal\Core\Session\AccountInterface $account
  66. * The account object whose grants are requested.
  67. * @param string $op
  68. * The node operation to be performed, such as 'view', 'update', or 'delete'.
  69. *
  70. * @return array
  71. * An array whose keys are "realms" of grants, and whose values are arrays of
  72. * the grant IDs within this realm that this user is being granted.
  73. *
  74. * @see node_access_view_all_nodes()
  75. * @see node_access_rebuild()
  76. * @ingroup node_access
  77. */
  78. function hook_node_grants(\Drupal\Core\Session\AccountInterface $account, $op) {
  79. if ($account->hasPermission('access private content')) {
  80. $grants['example'] = [1];
  81. }
  82. if ($account->id()) {
  83. $grants['example_author'] = [$account->id()];
  84. }
  85. return $grants;
  86. }
  87. /**
  88. * Set permissions for a node to be written to the database.
  89. *
  90. * When a node is saved, a module implementing hook_node_access_records() will
  91. * be asked if it is interested in the access permissions for a node. If it is
  92. * interested, it must respond with an array of permissions arrays for that
  93. * node.
  94. *
  95. * Node access grants apply regardless of the published or unpublished status
  96. * of the node. Implementations must make sure not to grant access to
  97. * unpublished nodes if they don't want to change the standard access control
  98. * behavior. Your module may need to create a separate access realm to handle
  99. * access to unpublished nodes.
  100. *
  101. * Note that the grant values in the return value from your hook must be
  102. * integers and not boolean TRUE and FALSE.
  103. *
  104. * Each permissions item in the array is an array with the following elements:
  105. * - 'realm': The name of a realm that the module has defined in
  106. * hook_node_grants().
  107. * - 'gid': A 'grant ID' from hook_node_grants().
  108. * - 'grant_view': If set to 1 a user that has been identified as a member
  109. * of this gid within this realm can view this node. This should usually be
  110. * set to $node->isPublished(). Failure to do so may expose unpublished content
  111. * to some users.
  112. * - 'grant_update': If set to 1 a user that has been identified as a member
  113. * of this gid within this realm can edit this node.
  114. * - 'grant_delete': If set to 1 a user that has been identified as a member
  115. * of this gid within this realm can delete this node.
  116. * - langcode: (optional) The language code of a specific translation of the
  117. * node, if any. Modules may add this key to grant different access to
  118. * different translations of a node, such that (e.g.) a particular group is
  119. * granted access to edit the Catalan version of the node, but not the
  120. * Hungarian version. If no value is provided, the langcode is set
  121. * automatically from the $node parameter and the node's original language (if
  122. * specified) is used as a fallback. Only specify multiple grant records with
  123. * different languages for a node if the site has those languages configured.
  124. *
  125. * A "deny all" grant may be used to deny all access to a particular node or
  126. * node translation:
  127. * @code
  128. * $grants[] = array(
  129. * 'realm' => 'all',
  130. * 'gid' => 0,
  131. * 'grant_view' => 0,
  132. * 'grant_update' => 0,
  133. * 'grant_delete' => 0,
  134. * 'langcode' => 'ca',
  135. * );
  136. * @endcode
  137. * Note that another module node access module could override this by granting
  138. * access to one or more nodes, since grants are additive. To enforce that
  139. * access is denied in a particular case, use hook_node_access_records_alter().
  140. * Also note that a deny all is not written to the database; denies are
  141. * implicit.
  142. *
  143. * @param \Drupal\node\NodeInterface $node
  144. * The node that has just been saved.
  145. *
  146. * @return
  147. * An array of grants as defined above.
  148. *
  149. * @see hook_node_access_records_alter()
  150. * @ingroup node_access
  151. */
  152. function hook_node_access_records(\Drupal\node\NodeInterface $node) {
  153. // We only care about the node if it has been marked private. If not, it is
  154. // treated just like any other node and we completely ignore it.
  155. if ($node->private->value) {
  156. $grants = [];
  157. // Only published Catalan translations of private nodes should be viewable
  158. // to all users. If we fail to check $node->isPublished(), all users would be able
  159. // to view an unpublished node.
  160. if ($node->isPublished()) {
  161. $grants[] = [
  162. 'realm' => 'example',
  163. 'gid' => 1,
  164. 'grant_view' => 1,
  165. 'grant_update' => 0,
  166. 'grant_delete' => 0,
  167. 'langcode' => 'ca',
  168. ];
  169. }
  170. // For the example_author array, the GID is equivalent to a UID, which
  171. // means there are many groups of just 1 user.
  172. // Note that an author can always view his or her nodes, even if they
  173. // have status unpublished.
  174. if ($node->getOwnerId()) {
  175. $grants[] = [
  176. 'realm' => 'example_author',
  177. 'gid' => $node->getOwnerId(),
  178. 'grant_view' => 1,
  179. 'grant_update' => 1,
  180. 'grant_delete' => 1,
  181. 'langcode' => 'ca',
  182. ];
  183. }
  184. return $grants;
  185. }
  186. }
  187. /**
  188. * Alter permissions for a node before it is written to the database.
  189. *
  190. * Node access modules establish rules for user access to content. Node access
  191. * records are stored in the {node_access} table and define which permissions
  192. * are required to access a node. This hook is invoked after node access modules
  193. * returned their requirements via hook_node_access_records(); doing so allows
  194. * modules to modify the $grants array by reference before it is stored, so
  195. * custom or advanced business logic can be applied.
  196. *
  197. * Upon viewing, editing or deleting a node, hook_node_grants() builds a
  198. * permissions array that is compared against the stored access records. The
  199. * user must have one or more matching permissions in order to complete the
  200. * requested operation.
  201. *
  202. * A module may deny all access to a node by setting $grants to an empty array.
  203. *
  204. * The preferred use of this hook is in a module that bridges multiple node
  205. * access modules with a configurable behavior, as shown in the example with the
  206. * 'is_preview' field.
  207. *
  208. * @param array $grants
  209. * The $grants array returned by hook_node_access_records().
  210. * @param \Drupal\node\NodeInterface $node
  211. * The node for which the grants were acquired.
  212. *
  213. * @see hook_node_access_records()
  214. * @see hook_node_grants()
  215. * @see hook_node_grants_alter()
  216. * @ingroup node_access
  217. */
  218. function hook_node_access_records_alter(&$grants, Drupal\node\NodeInterface $node) {
  219. // Our module allows editors to mark specific articles with the 'is_preview'
  220. // field. If the node being saved has a TRUE value for that field, then only
  221. // our grants are retained, and other grants are removed. Doing so ensures
  222. // that our rules are enforced no matter what priority other grants are given.
  223. if ($node->is_preview) {
  224. // Our module grants are set in $grants['example'].
  225. $temp = $grants['example'];
  226. // Now remove all module grants but our own.
  227. $grants = ['example' => $temp];
  228. }
  229. }
  230. /**
  231. * Alter user access rules when trying to view, edit or delete a node.
  232. *
  233. * Node access modules establish rules for user access to content.
  234. * hook_node_grants() defines permissions for a user to view, edit or delete
  235. * nodes by building a $grants array that indicates the permissions assigned to
  236. * the user by each node access module. This hook is called to allow modules to
  237. * modify the $grants array by reference, so the interaction of multiple node
  238. * access modules can be altered or advanced business logic can be applied.
  239. *
  240. * The resulting grants are then checked against the records stored in the
  241. * {node_access} table to determine if the operation may be completed.
  242. *
  243. * A module may deny all access to a user by setting $grants to an empty array.
  244. *
  245. * Developers may use this hook to either add additional grants to a user or to
  246. * remove existing grants. These rules are typically based on either the
  247. * permissions assigned to a user role, or specific attributes of a user
  248. * account.
  249. *
  250. * @param array $grants
  251. * The $grants array returned by hook_node_grants().
  252. * @param \Drupal\Core\Session\AccountInterface $account
  253. * The account requesting access to content.
  254. * @param string $op
  255. * The operation being performed, 'view', 'update' or 'delete'.
  256. *
  257. * @see hook_node_grants()
  258. * @see hook_node_access_records()
  259. * @see hook_node_access_records_alter()
  260. * @ingroup node_access
  261. */
  262. function hook_node_grants_alter(&$grants, \Drupal\Core\Session\AccountInterface $account, $op) {
  263. // Our sample module never allows certain roles to edit or delete
  264. // content. Since some other node access modules might allow this
  265. // permission, we expressly remove it by returning an empty $grants
  266. // array for roles specified in our variable setting.
  267. // Get our list of banned roles.
  268. $restricted = \Drupal::config('example.settings')->get('restricted_roles');
  269. if ($op != 'view' && !empty($restricted)) {
  270. // Now check the roles for this account against the restrictions.
  271. foreach ($account->getRoles() as $rid) {
  272. if (in_array($rid, $restricted)) {
  273. $grants = [];
  274. }
  275. }
  276. }
  277. }
  278. /**
  279. * Controls access to a node.
  280. *
  281. * Modules may implement this hook if they want to have a say in whether or not
  282. * a given user has access to perform a given operation on a node.
  283. *
  284. * The administrative account (user ID #1) always passes any access check, so
  285. * this hook is not called in that case. Users with the "bypass node access"
  286. * permission may always view and edit content through the administrative
  287. * interface.
  288. *
  289. * The access to a node can be influenced in several ways:
  290. * - To explicitly allow access, return an AccessResultInterface object with
  291. * isAllowed() returning TRUE. Other modules can override this access by
  292. * returning TRUE for isForbidden().
  293. * - To explicitly forbid access, return an AccessResultInterface object with
  294. * isForbidden() returning TRUE. Access will be forbidden even if your module
  295. * (or another module) also returns TRUE for isNeutral() or isAllowed().
  296. * - To neither allow nor explicitly forbid access, return an
  297. * AccessResultInterface object with isNeutral() returning TRUE.
  298. * - If your module does not return an AccessResultInterface object, neutral
  299. * access will be assumed.
  300. *
  301. * Also note that this function isn't called for node listings (e.g., RSS feeds,
  302. * the default home page at path 'node', a recent content block, etc.) See
  303. * @link node_access Node access rights @endlink for a full explanation.
  304. *
  305. * @param \Drupal\node\NodeInterface|string $node
  306. * Either a node entity or the machine name of the content type on which to
  307. * perform the access check.
  308. * @param string $op
  309. * The operation to be performed. Possible values:
  310. * - "create"
  311. * - "delete"
  312. * - "update"
  313. * - "view"
  314. * @param \Drupal\Core\Session\AccountInterface $account
  315. * The user object to perform the access check operation on.
  316. *
  317. * @return \Drupal\Core\Access\AccessResultInterface
  318. * The access result.
  319. *
  320. * @ingroup node_access
  321. */
  322. function hook_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
  323. $type = $node->bundle();
  324. switch ($op) {
  325. case 'create':
  326. return AccessResult::allowedIfHasPermission($account, 'create ' . $type . ' content');
  327. case 'update':
  328. if ($account->hasPermission('edit any ' . $type . ' content', $account)) {
  329. return AccessResult::allowed()->cachePerPermissions();
  330. }
  331. else {
  332. return AccessResult::allowedIf($account->hasPermission('edit own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);
  333. }
  334. case 'delete':
  335. if ($account->hasPermission('delete any ' . $type . ' content', $account)) {
  336. return AccessResult::allowed()->cachePerPermissions();
  337. }
  338. else {
  339. return AccessResult::allowedIf($account->hasPermission('delete own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);
  340. }
  341. default:
  342. // No opinion.
  343. return AccessResult::neutral();
  344. }
  345. }
  346. /**
  347. * Act on a node being displayed as a search result.
  348. *
  349. * This hook is invoked from the node search plugin during search execution,
  350. * after loading and rendering the node.
  351. *
  352. * @param \Drupal\node\NodeInterface $node
  353. * The node being displayed in a search result.
  354. *
  355. * @return array
  356. * Extra information to be displayed with search result. This information
  357. * should be presented as an associative array. It will be concatenated with
  358. * the post information (last updated, author) in the default search result
  359. * theming.
  360. *
  361. * @see template_preprocess_search_result()
  362. * @see search-result.html.twig
  363. *
  364. * @ingroup entity_crud
  365. */
  366. function hook_node_search_result(\Drupal\node\NodeInterface $node) {
  367. $rating = db_query('SELECT SUM(points) FROM {my_rating} WHERE nid = :nid', ['nid' => $node->id()])->fetchField();
  368. return ['rating' => \Drupal::translation()->formatPlural($rating, '1 point', '@count points')];
  369. }
  370. /**
  371. * Act on a node being indexed for searching.
  372. *
  373. * This hook is invoked during search indexing, after loading, and after the
  374. * result of rendering is added as $node->rendered to the node object.
  375. *
  376. * @param \Drupal\node\NodeInterface $node
  377. * The node being indexed.
  378. *
  379. * @return string
  380. * Additional node information to be indexed.
  381. *
  382. * @ingroup entity_crud
  383. */
  384. function hook_node_update_index(\Drupal\node\NodeInterface $node) {
  385. $text = '';
  386. $ratings = db_query('SELECT title, description FROM {my_ratings} WHERE nid = :nid', [':nid' => $node->id()]);
  387. foreach ($ratings as $rating) {
  388. $text .= '<h2>' . Html::escape($rating->title) . '</h2>' . Xss::filter($rating->description);
  389. }
  390. return $text;
  391. }
  392. /**
  393. * Provide additional methods of scoring for core search results for nodes.
  394. *
  395. * A node's search score is used to rank it among other nodes matched by the
  396. * search, with the highest-ranked nodes appearing first in the search listing.
  397. *
  398. * For example, a module allowing users to vote on content could expose an
  399. * option to allow search results' rankings to be influenced by the average
  400. * voting score of a node.
  401. *
  402. * All scoring mechanisms are provided as options to site administrators, and
  403. * may be tweaked based on individual sites or disabled altogether if they do
  404. * not make sense. Individual scoring mechanisms, if enabled, are assigned a
  405. * weight from 1 to 10. The weight represents the factor of magnification of
  406. * the ranking mechanism, with higher-weighted ranking mechanisms having more
  407. * influence. In order for the weight system to work, each scoring mechanism
  408. * must return a value between 0 and 1 for every node. That value is then
  409. * multiplied by the administrator-assigned weight for the ranking mechanism,
  410. * and then the weighted scores from all ranking mechanisms are added, which
  411. * brings about the same result as a weighted average.
  412. *
  413. * @return array
  414. * An associative array of ranking data. The keys should be strings,
  415. * corresponding to the internal name of the ranking mechanism, such as
  416. * 'recent', or 'comments'. The values should be arrays themselves, with the
  417. * following keys available:
  418. * - title: (required) The human readable name of the ranking mechanism.
  419. * - join: (optional) An array with information to join any additional
  420. * necessary table. This is not necessary if the table required is already
  421. * joined to by the base query, such as for the {node} table. Other tables
  422. * should use the full table name as an alias to avoid naming collisions.
  423. * - score: (required) The part of a query string to calculate the score for
  424. * the ranking mechanism based on values in the database. This does not need
  425. * to be wrapped in parentheses, as it will be done automatically; it also
  426. * does not need to take the weighted system into account, as it will be
  427. * done automatically. It does, however, need to calculate a decimal between
  428. * 0 and 1; be careful not to cast the entire score to an integer by
  429. * inadvertently introducing a variable argument.
  430. * - arguments: (optional) If any arguments are required for the score, they
  431. * can be specified in an array here.
  432. *
  433. * @ingroup entity_crud
  434. */
  435. function hook_ranking() {
  436. // If voting is disabled, we can avoid returning the array, no hard feelings.
  437. if (\Drupal::config('vote.settings')->get('node_enabled')) {
  438. return [
  439. 'vote_average' => [
  440. 'title' => t('Average vote'),
  441. // Note that we use i.sid, the search index's search item id, rather than
  442. // n.nid.
  443. 'join' => [
  444. 'type' => 'LEFT',
  445. 'table' => 'vote_node_data',
  446. 'alias' => 'vote_node_data',
  447. 'on' => 'vote_node_data.nid = i.sid',
  448. ],
  449. // The highest possible score should be 1, and the lowest possible score,
  450. // always 0, should be 0.
  451. 'score' => 'vote_node_data.average / CAST(%f AS DECIMAL)',
  452. // Pass in the highest possible voting score as a decimal argument.
  453. 'arguments' => [\Drupal::config('vote.settings')->get('score_max')],
  454. ],
  455. ];
  456. }
  457. }
  458. /**
  459. * Alter the links of a node.
  460. *
  461. * @param array &$links
  462. * A renderable array representing the node links.
  463. * @param \Drupal\node\NodeInterface $entity
  464. * The node being rendered.
  465. * @param array &$context
  466. * Various aspects of the context in which the node links are going to be
  467. * displayed, with the following keys:
  468. * - 'view_mode': the view mode in which the node is being viewed
  469. * - 'langcode': the language in which the node is being viewed
  470. *
  471. * @see \Drupal\node\NodeViewBuilder::renderLinks()
  472. * @see \Drupal\node\NodeViewBuilder::buildLinks()
  473. * @see entity_crud
  474. */
  475. function hook_node_links_alter(array &$links, NodeInterface $entity, array &$context) {
  476. $links['mymodule'] = [
  477. '#theme' => 'links__node__mymodule',
  478. '#attributes' => ['class' => ['links', 'inline']],
  479. '#links' => [
  480. 'node-report' => [
  481. 'title' => t('Report'),
  482. 'url' => Url::fromRoute('node_test.report', ['node' => $entity->id()], ['query' => ['token' => \Drupal::getContainer()->get('csrf_token')->get("node/{$entity->id()}/report")]]),
  483. ],
  484. ],
  485. ];
  486. }
  487. /**
  488. * @} End of "addtogroup hooks".
  489. */