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

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

namespace Drupal\improvements_paragraphs\Plugin\paragraphs\Behavior;

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\druhels\ArrayHelper;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;

/**
 * @TODO Rewrite to PHP-attribute
 */
/**
 * @ParagraphsBehavior(
 *   id = "paragraph_html_attributes_behavior",
 *   label = @Translation("Html attributes (deprecated)"),
 *   description = @Translation("Allows to set paragraph html attributes."),
 *   weight = 1000,
 * )
 */
class ParagraphAttributesBehavior extends ParagraphsBehaviorBase {

  /**
   * {@inheritdoc}
   */
  public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state): array {
    $attributes = $paragraph->getBehaviorSetting($this->pluginId, 'attributes', []);

    $form['attributes'] = [
      '#type' => 'textarea',
      '#title' => t('Html attributes (deprecated)'),
      '#description' => t('Example') . ' <code>class: my-paragraph-class</code>',
      '#default_value' => ArrayHelper::formatArrayAsKeyValueList($attributes),
      '#rows' => 2,
    ];

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

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state): void {
    $filtered_values = $this->filterBehaviorFormSubmitValues($paragraph, $form, $form_state);
    $filtered_values['attributes'] = !empty($filtered_values['attributes']) ? ArrayHelper::formatKeyValueListAsArray($filtered_values['attributes'], ': ') : [];
    $paragraph->setBehaviorSettings($this->pluginId, $filtered_values);
  }

  /**
   * {@inheritDoc}
   */
  public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode): void {
    if ($behavior_attributes = $paragraph->getBehaviorSetting($this->pluginId, 'attributes', [])) {
      $build['#attributes'] = array_merge($build['#attributes'] ?? [], ArrayHelper::formatArrayAsAttributes($behavior_attributes));
    }
  }

}

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

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