function paragraphs_library_paragraphs_widget_actions_alter

Implements hook_paragraphs_widget_actions_alter().

File

paragraphs/modules/paragraphs_library/paragraphs_library.module, line 44
Main module file for the Paragraphs Library module.

Code

function paragraphs_library_paragraphs_widget_actions_alter(&$widget_actions, &$context) {

  // Do not register conversions if structure changes are disallowed.
  if (!$context['allow_reference_changes']) {
    return;
  }
  $field_definition = $context['items']
    ->getFieldDefinition();
  $parents = $context['element']['#field_parents'];
  $field_name = $field_definition
    ->getName();
  $widget_state = $context['widget'];
  $delta = $context['delta'];
  $id_prefix = implode('_', array_merge($parents, [
    $field_name,
    $delta,
  ]));

  /** @var \Drupal\Paragraphs\ParagraphInterface $paragraphs_entity */
  $paragraphs_entity = $context['paragraphs_entity'];

  // Don't allow conversion of items not marked as allowed for conversion.
  $allow_library_conversion = $paragraphs_entity
    ->getParagraphType()
    ->getThirdPartySetting('paragraphs_library', 'allow_library_conversion', FALSE);
  if ($paragraphs_entity
    ->bundle() == 'from_library') {
    $widget_actions['dropdown_actions']['library_to_paragraph'] = [
      '#type' => 'submit',
      '#value' => t('Unlink from library'),
      '#name' => strtr($id_prefix, '-', '_') . '_unlink_from_library',
      '#weight' => 503,
      '#submit' => [
        'paragraphs_library_library_item_unlink_submit',
      ],
      '#limit_validation_errors' => [
        array_merge($parents, [
          $field_name,
          'add_more',
        ]),
      ],
      '#delta' => $delta,
      '#ajax' => [
        'callback' => [
          ParagraphsWidget::class,
          'itemAjax',
        ],
        'wrapper' => $widget_state['ajax_wrapper_id'],
        'effect' => 'fade',
      ],
      '#attributes' => [
        'class' => [
          'button--small',
        ],
      ],
    ];
  }
  elseif ($allow_library_conversion) {

    // Convert non-library items only.
    $widget_actions['dropdown_actions']['promote_to_library'] = [
      '#value' => t('Promote to library'),
      '#name' => $id_prefix . '_promote_to_library',
      '#weight' => 503,
      '#submit' => [
        'paragraphs_library_make_library_item_submit',
      ],
      '#limit_validation_errors' => [
        array_merge($parents, [
          $field_name,
          'promote_to_library',
        ]),
      ],
      '#delta' => $delta,
      '#ajax' => [
        'callback' => [
          ParagraphsWidget::class,
          'itemAjax',
        ],
        'wrapper' => $widget_state['ajax_wrapper_id'],
      ],
      '#access' => \Drupal::entityTypeManager()
        ->getAccessControlHandler('paragraphs_library_item')
        ->createAccess(),
      '#attributes' => [
        'class' => [
          'button--small',
        ],
      ],
    ];
  }
}