improvements-2.x-dev/modules/improvements_paragraphs/src/Plugin/paragraphs/Behavior/ViewModeParagraphBehavior.php

modules/improvements_paragraphs/src/Plugin/paragraphs/Behavior/ViewModeParagraphBehavior.php
<?php

namespace Drupal\improvements_paragraphs\Plugin\paragraphs\Behavior;

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;
use Drupal\paragraphs\Plugin\EntityReferenceSelection\ParagraphSelection;

/**
 * @ParagraphsBehavior(
 *   id = "view_mode",
 *   label = @Translation("View mode"),
 *   description = @Translation("Allows to select paragraph view mode."),
 *   weight = 102,
 * )
 */
class ViewModeParagraphBehavior extends ParagraphsBehaviorBase {

  /**
   * {@inheritdoc}
   */
  public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state): array {
    $entity_display_repository = \Drupal::service('entity_display.repository');

    $view_mode_options = $entity_display_repository->getViewModeOptionsByBundle($paragraph->getEntityTypeId(), $paragraph->bundle());
    if (count($view_mode_options) > 1) {
      $form['view_mode'] = [
        '#type' => 'select',
        '#title' => t('View mode'),
        '#options' => $entity_display_repository->getViewModeOptionsByBundle($paragraph->getEntityTypeId(), $paragraph->bundle()),
        '#empty_option' => $this->t('- None -'),
        '#default_value' => $paragraph->getBehaviorSetting($this->pluginId, 'view_mode'),
      ];
    }

    foreach ($paragraph->getFieldDefinitions() as $field_definition) {
      if ($field_definition->getType() == 'entity_reference_revisions') {
        $target_entity_type_id = $field_definition->getTargetEntityTypeId();
        $target_bundles = $this->getEntityReferenceFieldTargetBundles($field_definition);
        $child_view_mode_options = [];

        foreach ($target_bundles as $target_bundle) {
          $target_bundle_view_modes = $entity_display_repository->getViewModeOptionsByBundle($target_entity_type_id, $target_bundle);
          if (count($target_bundle_view_modes) > 1) {
            foreach ($target_bundle_view_modes as $target_bundle_view_mode_id => $target_bundle_view_mode_label) {
              $child_view_mode_options[$target_bundle][$target_bundle . '.' . $target_bundle_view_mode_id] = $target_bundle_view_mode_label;
            }
          }
        }

        if ($child_view_mode_options) {
          $form['child_view_mode'][$field_definition->getName()] = [
            '#type' => 'select',
            '#title' => t('View mode for entities in "@field" field', ['@field' => $field_definition->getLabel()]),
            '#options' => $child_view_mode_options,
            '#empty_option' => $this->t('- None -'),
            '#default_value' => $paragraph->getBehaviorSetting($this->pluginId, ['child_view_mode', $field_definition->getName()]),
          ];
        }
      }
    }

    if (isset($form['child_view_mode'])) {
      $form['child_view_mode']['#tree'] = TRUE;
    }

    $form['#weight'] = $this->pluginDefinition['weight'];

    return $form;
  }

  /**
   * {@inheritDoc}
   */
  public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode): void {
    //
  }

  /**
   * Return entity reference field target bundles.
   */
  protected function getEntityReferenceFieldTargetBundles(FieldDefinitionInterface $entity_reference_field_definition): array {
    $entity_reference_selection_manager = \Drupal::service('plugin.manager.entity_reference_selection');
    $entity_reference_field_selection_handler = $entity_reference_selection_manager->getSelectionHandler($entity_reference_field_definition);

    if ($entity_reference_field_selection_handler instanceof ParagraphSelection) {
      return array_keys($entity_reference_field_selection_handler->getSortedAllowedTypes());
    }

    return [];
  }

}

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

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