hn-8.x-1.x-dev/tests/modules/hn_test/hn_test.install

tests/modules/hn_test/hn_test.install
<?php

/**
 * @file
 * Provides an install function that creates a few test nodes.
 */

use Drupal\node\Entity\Node;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;

/**
 * This creates default fields based on a node id for testing.
 *
 * @param int $nodeId
 *   The nodeId to create default fields for.
 *
 * @return array
 *   The default fields.
 */
function hn_test_node_base($nodeId) {
  return [
    'type' => 'hn_test_basic_page',
    'title' => 'Node ' . $nodeId,
    'body' => 'Node ' . $nodeId . ' body',
    'field_teaser_body' => 'Node ' . $nodeId . ' teaser body',
  ];
}

/**
 * Implements hook_install().
 */
function hn_test_install() {

  // Give permissions to anonymous users.
  /** @var \Drupal\user\Entity\Role $anonymous */
  $anonymous = Role::load(RoleInterface::ANONYMOUS_ID);
  $anonymous->grantPermission('access hn');
  $anonymous->grantPermission('access content');
  $anonymous->save();

  // Create dummy content.
  $node1 = Node::create([
    'field_link' => 'https://www.google.com',
  ] + hn_test_node_base(1));
  $node1->save();

  $node2 = Node::create(
    [
      'field_link' => 'entity:node/1',
      'field_reference' => $node1,
    ] + hn_test_node_base(2)
  );
  $node2->save();

  $node3 = Node::create(
    [
      'field_link' => 'entity:node/1',
      'field_reference_teaser' => $node1,
    ] + hn_test_node_base(3)
  );
  $node3->save();

  $node4 = Node::create(
    [
      'field_reference' => $node2,
    ] + hn_test_node_base(4)
  );
  $node4->save();

  $node5 = Node::create([
    'field_reference' => $node1,
    'field_reference_teaser' => $node1,
  ] + hn_test_node_base(5));
  $node5->save();
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc