Skip to content
node_test.module 5.19 KiB
Newer Older
 * A dummy module for testing node related hooks.
 *
 * This is a dummy module that implements node related hooks to test API
 * interaction with the Node module.
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
 * Implements hook_ENTITY_TYPE_view() for node entities.
function node_test_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  if ($view_mode == 'rss') {
    // Add RSS elements and namespaces when building the RSS feed.
    $node->rss_elements[] = array(
      'key' => 'testElement',
      'value' => t('Value of testElement RSS element for node @nid.', array('@nid' => $node->id())),
    );

    // Add content that should be displayed only in the RSS feed.
    $build['extra_feed_content'] = array(
      '#markup' => '<p>' . t('Extra data that should appear only in the RSS feed for node @nid.', array('@nid' => $node->id())) . '</p>',
  if ($view_mode != 'rss') {
    // Add content that should NOT be displayed in the RSS feed.
    $build['extra_non_feed_content'] = array(
      '#markup' => '<p>' . t('Extra data that should appear everywhere except the RSS feed for node @nid.', array('@nid' => $node->id())) . '</p>',
 * Implements hook_ENTITY_TYPE_build_defaults_alter() for node entities.
function node_test_node_build_defaults_alter(array &$build, NodeInterface &$node, $view_mode = 'full') {
  if ($view_mode == 'rss') {
    $node->rss_namespaces['xmlns:drupaltest'] = 'http://example.com/test-namespace';
  }
}

function node_test_node_grants(AccountInterface $account, $op) {
  // Give everyone full grants so we don't break other node tests.
  // Our node access tests asserts three realms of access.
  return array(
    'test_article_realm' => array(1),
    'test_page_realm' => array(1),
    'test_alter_realm' => array(2),
  );
}

/**
 * Implements hook_node_access_records().
function node_test_node_access_records(NodeInterface $node) {
  // Return nothing when testing for empty responses.
  if (!empty($node->disable_node_access)) {
    return;
  }
  if ($node->getType() == 'article') {
    // Create grant in arbitrary article_realm for article nodes.
    $grants[] = array(
      'realm' => 'test_article_realm',
      'gid' => 1,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => 0,
    );
  }
  elseif ($node->getType() == 'page') {
    // Create grant in arbitrary page_realm for page nodes.
    $grants[] = array(
      'realm' => 'test_page_realm',
      'gid' => 1,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => 0,
    );
  }
  return $grants;
}

/**
 * Implements hook_node_access_records_alter().
function node_test_node_access_records_alter(&$grants, NodeInterface $node) {
  if (!empty($grants)) {
    foreach ($grants as $key => $grant) {
      // Alter grant from test_page_realm to test_alter_realm and modify the gid.
      if ($grant['realm'] == 'test_page_realm' && $node->isPromoted()) {
        $grants[$key]['realm'] = 'test_alter_realm';
        $grants[$key]['gid'] = 2;
      }
    }
  }
}

/**
function node_test_node_grants_alter(&$grants, AccountInterface $account, $op) {
  // Return an empty array of grants to prove that we can alter by reference.
  $grants = array();
}
 * Implements hook_ENTITY_TYPE_presave() for node entities.
function node_test_node_presave(NodeInterface $node) {
  if ($node->getTitle() == 'testing_node_presave') {
    $node->setCreatedTime(280299600);
  if (!empty($node->original) && $node->original->getTitle() == 'test_changes') {
    if ($node->original->getTitle() != $node->getTitle()) {
 * Implements hook_ENTITY_TYPE_update() for node entities.
function node_test_node_update(NodeInterface $node) {
  if (!empty($node->original) && $node->original->getTitle() == 'test_changes') {
    if ($node->original->getTitle() != $node->getTitle()) {
function node_test_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
  // Only alter the view mode if we are on the test callback.
  $change_view_mode = \Drupal::state()->get( 'node_test_change_view_mode') ?: '';
  if ($change_view_mode) {
 * Implements hook_ENTITY_TYPE_insert() for node entities.
 *
 * This tests saving a node on node insert.
 *
 * @see \Drupal\node\Tests\NodeSaveTest::testNodeSaveOnInsert()
 */
function node_test_node_insert(NodeInterface $node) {
  // Set the node title to the node ID and save.
  if ($node->getTitle() == 'new') {
    $node->setTitle('Node ' . $node->id());