You are here

trait BulkCreationEntityFormTrait in Lightning Core 8.4

Same name and namespace in other branches
  1. 8.5 src/Form/BulkCreationEntityFormTrait.php \Drupal\lightning_core\Form\BulkCreationEntityFormTrait
  2. 8 src/Form/BulkCreationEntityFormTrait.php \Drupal\lightning_core\Form\BulkCreationEntityFormTrait
  3. 8.2 src/Form/BulkCreationEntityFormTrait.php \Drupal\lightning_core\Form\BulkCreationEntityFormTrait
  4. 8.3 src/Form/BulkCreationEntityFormTrait.php \Drupal\lightning_core\Form\BulkCreationEntityFormTrait

Implements a redirect chain in entity forms for bulk entity creation.

Hierarchy

File

src/Form/BulkCreationEntityFormTrait.php, line 11

Namespace

Drupal\lightning_core\Form
View source
trait BulkCreationEntityFormTrait {

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    parent::save($form, $form_state);
    $query = $this
      ->getRequest()->query;
    if ($query
      ->has('bulk_create')) {

      /** @var \Drupal\Core\Entity\EntityInterface $entity */
      $entity = $this
        ->getEntity();

      // If there are more entities to create, redirect to the edit form for the
      // next one in line.
      $queue = $query
        ->get('bulk_create', []);
      if (is_array($queue)) {
        $id = array_shift($queue);
        $redirect = $this->entityTypeManager
          ->getStorage($entity
          ->getEntityTypeId())
          ->load($id)
          ->toUrl('edit-form', [
          'query' => [
            'bulk_create' => $queue ?: TRUE,
          ],
        ]);
        $form_state
          ->setRedirectUrl($redirect);
      }
      else {
        try {
          $form_state
            ->setRedirectUrl($entity
            ->toUrl('collection'));
        } catch (UndefinedLinkTemplateException $e) {

          // The entity type does not declare a collection, so don't do
          // anything.
        }
      }
    }
  }

}

Members