improvements-2.x-dev/modules/improvements_paragraphs/improvements_paragraphs.module

modules/improvements_paragraphs/improvements_paragraphs.module
<?php

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs_library\LibraryItemInterface;

/**
 * Implements hook_entity_base_field_info().
 */
function improvements_paragraphs_entity_base_field_info(EntityTypeInterface $entity_type): array {
  $fields = [];

  if ($entity_type->id() == 'paragraph') {
    $fields['label'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Label'));

    $fields['machine_name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Machine name'))
      ->addConstraint('UniqueField');
  }

  return $fields;
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function improvements_paragraphs_theme_suggestions_paragraph_alter(array &$suggestions, array $variables): void {
  $paragraph = $variables['elements']['#paragraph']; /** @var ParagraphInterface $paragraph */

  if ($machine_name = $paragraph->get('machine_name')->value) {
    $suggestions[] = 'paragraph__' . $paragraph->bundle() . '__' . $machine_name;
  }
}

/**
 * Implements hook_entity_view_mode_alter().
 */
function improvements_paragraphs_entity_view_mode_alter(string &$view_mode, EntityInterface $entity): void {
  if ($entity instanceof ParagraphInterface) {
    // Change view mode by parent paragraph behavior
    $parent_entity = $entity->getParentEntity();
    if ($parent_entity instanceof ParagraphInterface) {
      $parent_paragraph_field_name = $entity->get('parent_field_name')->value;
      $parent_paragraph_behavior_settings = $parent_entity->getAllBehaviorSettings(); // Not used getBehaviorSetting() for increase performance
      if ($new_view_mode = $parent_paragraph_behavior_settings['view_mode']['child_view_mode'][$parent_paragraph_field_name] ?? NULL) {
        [$new_view_mode_bundle, $new_view_mode] = explode('.', $new_view_mode);
        if ($new_view_mode_bundle == $entity->bundle()) {
          $view_mode = $new_view_mode;
        }
      }
    }

    // Change view mode by current paragraph behavior
    $current_paragraph_behavior_settings = $entity->getAllBehaviorSettings(); // Not used getBehaviorSetting() for increase performance
    if ($new_view_mode = $current_paragraph_behavior_settings['view_mode']['view_mode'] ?? NULL) {
      $view_mode = $new_view_mode;
    }
  }
}

/**
 * Implements hook_paragraphs_widget_actions_alter().
 */
function improvements_paragraphs_paragraphs_widget_actions_alter(array &$widget_actions, array &$context): void {
  $paragraph = $context['paragraphs_entity'];  /** @var ParagraphInterface $paragraph */

  // Add link to edit paragraph in dropdown actions
  if (
    $paragraph->bundle() == 'from_library' &&
    $library_item_entity = $paragraph->get('field_reusable_paragraph')->entity /** @var LibraryItemInterface $library_item_entity */
  ) {
    $widget_actions['dropdown_actions']['edit_library_item'] = [
      '#type' => 'link',
      '#title' => t('Edit library item'),
      '#url' => $library_item_entity->toUrl('edit-form'),
      '#attributes' => [
        'target' => '_blank',
        'class' => ['button', 'button--small'],
      ],
      '#weight' => 1000,
    ];
  }
}

/**
 * Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter(): paragraphs.
 */
function improvements_paragraphs_field_widget_single_element_paragraphs_form_alter(array &$element, FormStateInterface $form_state, array $context): void {
  if (isset($element['behavior_plugins']['move_widget'])) {
    foreach ($element['behavior_plugins']['move_widget']['#behavior_configuration']['fields'] as $field_name => $weight) {
      if (isset($element['subform'][$field_name])) {
        $element['behavior_plugins'][$field_name] = $element['subform'][$field_name];
        $element['behavior_plugins'][$field_name]['#weight'] = $weight;
        $element['behavior_plugins'][$field_name]['#attributes']['class'][] = 'paragraphs-behavior';
        unset($element['subform'][$field_name]);
      }
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc