You are here

function devel_generate_entity_insert in Devel 8

Same name and namespace in other branches
  1. 8.3 devel_generate/devel_generate.module \devel_generate_entity_insert()
  2. 8.2 devel_generate/devel_generate.module \devel_generate_entity_insert()
  3. 4.x devel_generate/devel_generate.module \devel_generate_entity_insert()

Implements hook_entity_insert().

Inserts nodes properly based on generation options.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The base node created on submit. Inspects $node->devel_generate.

File

devel_generate/devel_generate.module, line 34

Code

function devel_generate_entity_insert(EntityInterface $entity) {
  if ($entity
    ->getEntityTypeId() != 'node' || !isset($entity->devel_generate)) {
    return;
  }

  /** @var \Drupal\node\NodeInterface $entity */
  $results = $entity->devel_generate;
  if (!empty($results['max_comments'])) {
    foreach ($entity
      ->getFieldDefinitions() as $field_name => $field_definition) {
      if ($field_definition
        ->getType() == 'comment' && $entity
        ->get($field_name)->status == CommentItemInterface::OPEN) {

        // Add comments for each comment field on entity.
        devel_generate_add_comments($entity, $field_definition, $results['users'], $results['max_comments'], $results['title_length']);
      }
    }
  }

  // Add an url alias. Cannot happen before save because we don't know the nid.
  if (!empty($results['add_alias'])) {
    $path = array(
      'source' => '/node/' . $entity
        ->id(),
      'alias' => '/node-' . $entity
        ->id() . '-' . $entity
        ->bundle(),
    );
    \Drupal::service('path.alias_storage')
      ->save($path['source'], $path['alias'], $entity
      ->language()
      ->getId());
  }

  // Add node statistics.
  if (!empty($results['add_statistics']) && \Drupal::moduleHandler()
    ->moduleExists('statistics')) {
    devel_generate_add_statistics($entity);
  }
}