You are here

paragraphs_inline_entity_form.module in Paragraphs Inline Entity Form 8

Module.

File

paragraphs_inline_entity_form.module
View source
<?php

/**
 * @file Module.
 */
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\entity_embed\Form\EntityEmbedDialog;

/**
 * Implements hook_form_alter().
 */
function paragraphs_inline_entity_form_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /* @var Drupal\Core\Entity\FieldableEntityInterface $entity */
  $formObject = $form_state
    ->getFormObject();
  if ($formObject instanceof EntityFormInterface) {
    $form['#attached']['library'][] = 'paragraphs_inline_entity_form/dialog';
  }
  if ($formObject instanceof EntityEmbedDialog) {
    if (paragraphs_inline_entity_form_entity_browser_is_paragraph_item($form)) {
      $form['#attached']['library'][] = 'paragraphs_inline_entity_form/dialog';
      switch ($form_state
        ->get('step')) {
        case 'select':

          // do nothing.
          break;
        case 'embed':

          // Change the back button to link to the paragraph edit form.
          $entity_uuid = $form['attributes']['data-entity-uuid']['#value'];
          $entity_type_manager = \Drupal::service('entity_type.manager');
          $entity = $entity_type_manager
            ->getStorage('paragraph')
            ->loadByProperties([
            'uuid' => $entity_uuid,
          ]);
          $paragraph = current($entity);
          $form['actions']['back'] = [
            '#type' => 'submit',
            '#value' => t('Edit paragraph'),
            '#submit' => [],
            '#attributes' => [
              'onclick' => [
                'window.ckeditorSaveCallback=Drupal.ckeditor.saveCallback',
              ],
            ],
            '#ajax' => [
              'url' => Url::fromRoute('entity_browser.edit_form', [
                'entity_type' => 'paragraph',
                'entity' => $paragraph
                  ->id(),
              ]),
              'event' => 'click',
            ],
          ];
          break;
      }
    }
  }
}

/**
 * Checks that the browser type is a paragraph_item.
 *
 * @param $form The form.
 * @return TRUE If it is a paragraph_item entity browser.
 */
function paragraphs_inline_entity_form_entity_browser_is_paragraph_item($form) {
  if (isset($form['entity_browser']['#entity_browser']) && $form['entity_browser']['#entity_browser'] == 'paragraph_items') {
    return TRUE;
  }
  if (isset($form['attributes']['data-embed-button']) && $form['attributes']['data-embed-button']['#value'] == 'paragraphs_inline_entity_form') {
    return TRUE;
  }
}

Functions

Namesort descending Description
paragraphs_inline_entity_form_entity_browser_is_paragraph_item Checks that the browser type is a paragraph_item.
paragraphs_inline_entity_form_form_alter Implements hook_form_alter().