Моя сборка сайта и модулей Друпал Часть 10 Patch, Paragraph Behaviour Modeview

<?php

namespace Drupal\uv_paragraph\Plugin\paragraphs\Behavior;

use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\Annotation\ParagraphsBehavior;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;

/**
 * Class ClassBehavior
 *
 * @package Drupal\uv_paragraph\Plugin\paragraphs\Behavior
 * @ParagraphsBehavior(
 *   id = "uv_media_image_behavior",
 *   label = "Media Image",
 *   description= "Add Options To Media or Image Reference Entity",
 *   weight= 1
 *
 * )
 */
class MediaImageBehaviour extends ParagraphsBehaviorBase {

  public static function isApplicable(ParagraphsType $paragraphs_type) {
    return $paragraphs_type->id() == 'images';
  }

  public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode) {
    $mediaGrid = $paragraph->getBehaviorSetting($this->getPluginId(), "media_grid", 1);
    $mediaStyle = $paragraph->getBehaviorSetting($this->getPluginId(), "media_style", '480x320');
    $build['#attributes']['class'][] = Html::getClass("media-grid");
    $build['#attributes']['class'][] = Html::getClass("media-grid-".$mediaGrid);
    $build['#attached']['library'][] = 'uv_paragraph/uv_paragraph_media';

    $imageFormatter = $build['field_images']['#formatter'];
    if($imageFormatter == 'media_thumbnail'){
      for($i =0; $i < $paragraph->get('field_images')->count(); $i++){
        $build['field_images'][$i]['#image_style'] = $mediaStyle;
      }
    }else if($imageFormatter == 'photoswipe_field_formatter'){
      for($i =0; $i < $paragraph->get('field_images')->count(); $i++){
         $build['field_images'][$i]['#display_settings']['photoswipe_node_style'] = $mediaStyle;
      }
    }
  }
  public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {

    $form['media_grid'] = [
      '#type' =>'select',
      '#title' => 'Grid',
      '#options' => [
        1 => 1,
        2 => 2,
        3 => 3,
      ],
      '#default_value' => $paragraph->getBehaviorSetting($this->getPluginId(), "media_grid", '480x320')
    ];
    $form['media_style'] = [
      '#type' => 'select',
      '#title' => 'Image Style',
      '#options' => [
        '320x480' => '320x480',
        '480x320' => '480x320'
      ]
    ];
    //TODO change media view reference for entity
    return $form;
  }

  public function settingsSummary(Paragraph $paragraph) {
    $summary = [];
    $mediaGrid = $paragraph->getBehaviorSetting($this->getPluginId(), "media_grid", 1);
    $mediaStyle = $paragraph->getBehaviorSetting($this->getPluginId(), "media_style", '480x320');
    $summary[] = "Media use: ". $mediaGrid;
    $summary[] = "Media Style: ". $mediaStyle;
    return $summary;
  }



}

 

Моя сборка сайта и модулей Друпал Часть 9
Моя сборка сайта и модулей Друпал (Часть 11) магия hook_preprocess_block
Советую почитать