You are here

multi_node_add.module in Multi Node Add 8

Same filename and directory in other branches
  1. 6 multi_node_add.module
  2. 7 multi_node_add.module

Allows to create multiple nodes using one page, one form submission.

Unusable without Javascript, there's no degradation implemented.

File

multi_node_add.module
View source
<?php

/**
 * @file
 * Allows to create multiple nodes using one page, one form submission.
 *
 * Unusable without Javascript, there's no degradation implemented.
 */
use Drupal\Core\Link;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_theme().
 */
function multi_node_add_theme() {
  return [
    'multi_node_add_list' => [
      'variables' => [
        'content' => NULL,
      ],
      'template' => 'multi-node-add-list',
    ],
  ];
}

/**
 * Prepares variables for list of available node type templates.
 *
 * Default template: node-add-list.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - content: An array of content types.
 *
 * @see node_add_page()
 */
function template_preprocess_multi_node_add_list(array &$variables) {
  $variables['types'] = [];
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $type) {
      $variables['types'][$type
        ->getOriginalId()] = [
        'type' => $type
          ->getOriginalId(),
        'add_link' => Link::createFromRoute($type
          ->label(), 'multi_node_add.add', [
          'node_type' => $type
            ->getOriginalId(),
        ]),
        'description' => [
          '#markup' => $type
            ->get('description'),
        ],
      ];
    }
  }
}

/**
 * Implements hook_form_alter().
 *
 * Modifies the node form according to the multi_node_add_page() form config.
 */
function multi_node_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $path_args = explode('/', \Drupal::service('path.current')
    ->getPath());
  if ($form_state
    ->has('multi_node_add_hijacked') || $form_state
    ->has('multi_node_add_fields_show')) {
    $form['#after_build'][] = 'multi_node_add_node_form_after_build';
    $form['actions']['submit']['#submit'][] = 'multi_node_add_node_form_submit';
    if (!$form_state
      ->has('multi_node_add_fields_show')) {
      $form_state
        ->set('multi_node_add_fields_show', explode(',', $path_args[4]));
    }
  }
}

/**
 * Modifies the node form according to the choices of the user.
 */
function multi_node_add_node_form_after_build($form, FormStateInterface $form_state) {
  $path_args = explode('/', \Drupal::service('path.current')
    ->getPath());
  unset($form['actions']['preview']);
  $form['actions']['submit']['#value'] = t('Create');
  unset($form['advanced']);
  $field_names = array_keys($form);
  $fields = \Drupal::entityManager()
    ->getFieldDefinitions('node', $path_args[2]);
  foreach ($field_names as $field_name) {
    if (isset($fields[$field_name]) && !in_array($field_name, $form_state
      ->get('multi_node_add_fields_show'))) {
      $form[$field_name]['#access'] = FALSE;
    }
  }
  return $form;
}

/**
 * Redirects the iFrame to the simple result page instead of full node page.
 *
 * @todo: make it work, even not called actually.
 */
function multi_node_add_node_form_submit($form, FormStateInterface $form_state) {

  // $path_args = explode('/', \Drupal::service('path.current')->getPath());
  // $form_state->setRedirect('multi_node_add.status', ['node_type' => $path_args[3], 'nid' => $form_state->get('nid')]);
  // drupal_get_messages(NULL, TRUE);
}

Functions

Namesort descending Description
multi_node_add_form_alter Implements hook_form_alter().
multi_node_add_node_form_after_build Modifies the node form according to the choices of the user.
multi_node_add_node_form_submit Redirects the iFrame to the simple result page instead of full node page.
multi_node_add_theme Implements hook_theme().
template_preprocess_multi_node_add_list Prepares variables for list of available node type templates.