improvements-2.x-dev/src/Plugin/Filter/AutoAttachLibrariesFilter.php

src/Plugin/Filter/AutoAttachLibrariesFilter.php
<?php

namespace Drupal\improvements\Plugin\Filter;

use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\druhels\ArrayHelper;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;

/**
 * @Filter(
 *   id = "auto_attach_libraries_filter",
 *   title = @Translation("Auto attach libraries"),
 *   description = @Translation("Attach libraries by elements."),
 *   type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
 *   settings = {
 *     "by_text": {},
 *     "by_xpath": {},
 *   },
 * )
 */
class AutoAttachLibrariesFilter extends FilterBase {

  /**
   * {@inheritDoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state): array {
    $form['by_text'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Attach libraries by text'),
      '#description' => $this->t('Recommended method. Format: "<code>string => library-name</code>". Example: "<code>use-ajax => core/drupal.ajax</code>"'),
      '#default_value' => ArrayHelper::formatArrayAsKeyValueList($this->settings['by_text'], ' => '),
    ];

    $form['by_xpath'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Attach libraries by xpath'),
      '#description' => $this->t('Not-recommended method due to lower performance. Format: "<code>xpath => library-name</code>". Example: "<code>//*[contains(@class, \'use-ajax\')] => core/drupal.ajax</code>"'),
      '#default_value' => ArrayHelper::formatArrayAsKeyValueList($this->settings['by_xpath'], ' => '),
    ];

    return $form;
  }

  /**
   * {@inheritDoc}
   */
  public function setConfiguration(array $configuration): self {
    if (is_string($configuration['settings']['by_text'])) {
      $configuration['settings']['by_text'] = ArrayHelper::formatKeyValueListAsArray($configuration['settings']['by_text'], ' => ');
    }
    if (is_string($configuration['settings']['by_xpath'])) {
      $configuration['settings']['by_xpath'] = ArrayHelper::formatKeyValueListAsArray($configuration['settings']['by_xpath'], ' => ');
    }

    return parent::setConfiguration($configuration);
  }

  /**
   * {@inheritDoc}
   */
  public function process($text, $langcode): FilterProcessResult {
    $result = new FilterProcessResult($text);
    $attached_libraries = [];

    // By text
    if ($this->settings['by_text']) {
      foreach ($this->settings['by_text'] as $string => $library_name) {
        if (str_contains($text, $string)) {
          $attached_libraries[] = $library_name;
        }
      }
    }

    // By xpath
    if ($this->settings['by_xpath']) {
      $dom = Html::load($text);
      $xpath = new \DOMXPath($dom);

      foreach ($this->settings['by_xpath'] as $query => $library_name) {
        if ($xpath->query($query)) {
          $attached_libraries[] = $library_name;
        }
      }
    }

    if ($attached_libraries) {
      $result->addAttachments(['library' => array_unique($attached_libraries)]);
    }

    return $result;
  }

}

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

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