You are here

paragraphs_frontend_ui.module in Paragraphs frontend ui 8.2

Same filename and directory in other branches
  1. 8 paragraphs_frontend_ui.module

Provides common hooks and alterations for paragraphs_frontend_ui module.

File

paragraphs_frontend_ui.module
View source
<?php

/**
 * @file
 * Provides common hooks and alterations for paragraphs_frontend_ui module.
 */
use Drupal\paragraphs_edit\Form\ParagraphEditForm;
use Drupal\paragraphs_frontend_ui\Form\SettingsForm;

/**
 * Implements hook_page_attachments().
 *
 * @param array $page
 */
function paragraphs_frontend_ui_page_attachments(array &$page) {
  $config = \Drupal::config(SettingsForm::SETTINGS_NAME);
  if ($config
    ->get('enable_nested_paragraphs')) {
    $page['#attached']['library'][] = 'paragraphs_frontend_ui/ajax_command_clear_contextual_links';
  }
}

/**
 * Implements hook_contextual_links_view_alter().
 *
 * Change some contextual links into off_canvas links.
 */
function paragraphs_frontend_ui_contextual_links_view_alter(&$element, $items) {
  $config = \Drupal::config(SettingsForm::SETTINGS_NAME);
  if (isset($element['#links']['paragraphs-editedit-form'])) {
    $element['#links']['paragraphs-editedit-form']['title'] = t('Edit content');
    if ($config
      ->get('pfui_edit_content_dialog') == 'off_canvas') {
      $element['#links']['paragraphs-editedit-form']['attributes'] = [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'dialog',
        'data-dialog-renderer' => 'off_canvas',
        'data-outside-in-edit' => TRUE,
        'data-dialog-options' => json_encode([
          'width' => '50%',
        ]),
      ];
      $element['#attached']['library'][] = 'outside_in/drupal.off_canvas';
    }
    else {
      $element['#links']['paragraphs-editedit-form']['attributes'] = [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => json_encode([
          'width' => '50%',
        ]),
      ];
    }
  }

  // Set the title for 'Clone' to 'Duplicate'.
  if (isset($element['#links']['paragraphs-editclone-form'])) {
    unset($element['#links']['paragraphs-editclone-form']);
  }
  if (isset($element['#links']['paragraphs-editdelete-form'])) {

    // Open the delete form inside a modal.
    $element['#links']['paragraphs-editdelete-form']['attributes'] = [
      'class' => [
        'use-ajax',
      ],
      'data-dialog-type' => 'modal',
    ];
  }
  if (isset($element['#links']['paragraphs-frontend-uiedit-settings'])) {
    $element['#attached']['library'][] = 'outside_in/drupal.off_canvas';
    $element['#attached']['library'][] = 'core/drupal.ajax';
    $element['#attached']['library'][] = 'core/drupal.dialog.ajax';
    if ($config
      ->get('pfui_edit_settings_dialog') == 'off_canvas') {
      $element['#links']['paragraphs-frontend-uiedit-settings']['attributes'] = [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'dialog',
        'data-dialog-renderer' => 'off_canvas',
        'data-outside-in-edit' => TRUE,
      ];
    }
    else {
      $element['#links']['paragraphs-frontend-uiedit-settings']['attributes'] = [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
      ];
    }
  }
  if (isset($element['#links']['paragraphs-frontend-uimove-up'])) {
    $element['#links']['paragraphs-frontend-uimove-up']['attributes'] = [
      'class' => [
        'use-ajax',
      ],
    ];
  }
  if (isset($element['#links']['paragraphs-frontend-uimove-down'])) {
    $element['#links']['paragraphs-frontend-uimove-down']['attributes'] = [
      'class' => [
        'use-ajax',
      ],
    ];
  }
  if (isset($element['#links']['paragraphs-frontend-uiduplicate'])) {
    $element['#links']['paragraphs-frontend-uiduplicate']['attributes'] = [
      'class' => [
        'use-ajax',
      ],
    ];
  }
  if (isset($element['#links']['paragraphs-frontend-uiadd'])) {
    if ($config
      ->get('pfui_add_below_dialog') == 'off_canvas') {
      $element['#links']['paragraphs-frontend-uiadd']['attributes'] = [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'dialog',
        'data-dialog-renderer' => 'off_canvas',
        'data-outside-in-edit' => TRUE,
      ];
      $element['#attached']['library'][] = 'outside_in/drupal.off_canvas';
    }
    else {
      $element['#links']['paragraphs-frontend-uiadd']['attributes'] = [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => json_encode([
          'width' => '50%',
        ]),
      ];
    }
  }
}

/**
 * Implements hook_entity_type_build().
 */
function paragraphs_frontend_ui_entity_type_build(array &$entity_types) {
  $entity_types['paragraph']
    ->setFormClass('settings', ParagraphEditForm::class);
  $entity_types['paragraph']
    ->setFormClass('content', ParagraphEditForm::class);
}

/**
 * Implements hook_preprocess_HOOK().
 *
 * Using hook_preprocess_field().
 */
function paragraphs_frontend_ui_preprocess_field(&$vars) {
  if (empty($vars['field_type']) || $vars['field_type'] !== 'entity_reference_revisions') {
    return;
  }
  if (Drupal::currentUser()
    ->isAuthenticated()) {
    $vars['attributes']['data-paragraphs-frontend-ui'] = $vars['field_name'] . '-' . $vars['element']['#object']
      ->id();
  }
}