You are here

function paragraphs_inline_entity_form_form_alter in Paragraphs Inline Entity Form 8

Same name and namespace in other branches
  1. 2.x paragraphs_inline_entity_form.module \paragraphs_inline_entity_form_form_alter()
  2. 2.0.x paragraphs_inline_entity_form.module \paragraphs_inline_entity_form_form_alter()

Implements hook_form_alter().

File

./paragraphs_inline_entity_form.module, line 14
Module.

Code

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;
      }
    }
  }
}