You are here

class DomApplyStyles in Migrate Plus 8.5

Apply Editor styles to configured elements.

Replace HTML elements with elements and classes specified in the Styles menu of the WYSIWYG editor.

Available configuration keys:

  • format: the text format to inspect for style options (optional, defaults to 'basic_html').
  • rules: an array of keyed arrays, with the following keys:
    • xpath: an XPath expression for the elements to replace.
    • style: the label of the item in the Styles menu to use.
    • depth: the number of parent elements to remove (optional, defaults to 0).

Example:


process:
  'body/value':
    -
      plugin: dom
      method: import
      source: 'body/0/value'
    -
      plugin: dom_apply_styles
      format: full_html
      rules:
        -
          xpath: '//b'
          style: Bold
        -
          xpath: '//span/i'
          style: Italic
          depth: 1
    -
      plugin: dom
      method: export

This will replace <b>...</b> with whatever style is labeled "Bold" in the Full HTML text format, perhaps <strong class="foo">...</strong>. It will also replace <span><i>...</i></span> with the style labeled "Italic" in that text format, perhaps <em class="foo bar">...</em>. You may get unexpected results if there is anything between the two opening tags or between the two closing tags. That is, the code assumes that '<span><i>' is closed with '</i></span>' exactly.

Plugin annotation


@MigrateProcessPlugin(
  id = "dom_apply_styles"
)

Hierarchy

Expanded class hierarchy of DomApplyStyles

1 file declares its use of DomApplyStyles
DomApplyStylesTest.php in tests/src/Unit/process/DomApplyStylesTest.php

File

src/Plugin/migrate/process/DomApplyStyles.php, line 64

Namespace

Drupal\migrate_plus\Plugin\migrate\process
View source
class DomApplyStyles extends DomProcessBase implements ContainerFactoryPluginInterface {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Array of styles from the WYSIWYG editor.
   *
   * @var array
   */
  protected $styles = [];

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactory $config_factory) {
    $configuration += [
      'format' => 'basic_html',
    ];
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $config_factory;
    $this
      ->setStyles($configuration['format']);
    $this
      ->validateRules();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $this
      ->init($value, $destination_property);
    foreach ($this->configuration['rules'] as $rule) {
      $this
        ->apply($rule);
    }
    return $this->document;
  }

  /**
   * Retrieve the list of styles based on configuration.
   *
   * The styles configuration is a string: styles are separated by "\r\n", and
   * each one has the format 'element(\.class)*|label'.
   * Convert this to an array with 'label' => 'element.class', and save as
   * $this->styles.
   *
   * @param string $format
   *   The text format from which to get configured styles.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  protected function setStyles($format) {
    if (empty($format) || !is_string($format)) {
      $message = 'The "format" option must be a non-empty string.';
      throw new InvalidPluginDefinitionException($this
        ->getPluginId(), $message);
    }
    $editor_styles = $this->configFactory
      ->get("editor.editor.{$format}")
      ->get('settings.plugins.stylescombo.styles');
    foreach (explode("\r\n", $editor_styles) as $rule) {
      if (preg_match('/(.*)\\|(.*)/', $rule, $matches)) {
        $this->styles[$matches[2]] = $matches[1];
      }
    }
  }

  /**
   * Validate the configured rules.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  protected function validateRules() {
    if (!array_key_exists('rules', $this->configuration) || !is_array($this->configuration['rules'])) {
      $message = 'The "rules" option must be an array.';
      throw new InvalidPluginDefinitionException($this
        ->getPluginId(), $message);
    }
    foreach ($this->configuration['rules'] as $rule) {
      if (empty($rule['xpath']) || empty($rule['style'])) {
        $message = 'The "xpath" and "style" options are required for each rule.';
        throw new InvalidPluginDefinitionException($this
          ->getPluginId(), $message);
      }
      if (empty($this->styles[$rule['style']])) {
        $message = sprintf('The style "%s" is not defined.', $rule['style']);
        throw new InvalidPluginDefinitionException($this
          ->getPluginId(), $message);
      }
    }
  }

  /**
   * Apply a rule to the document.
   *
   * Search $this->document for elements matching 'xpath' and replace them with
   * the HTML elements and classes in $this->styles specified by 'style'.
   * If 'depth' is positive, then replace additional parent elements as well.
   *
   * @param string[] $rule
   *   An array with keys 'xpath', 'style', and (optional) 'depth'.
   */
  protected function apply(array $rule) {

    // An entry in $this->styles has the format element(\.class)*: for example,
    // 'p' or 'a.button' or 'div.col-xs-6.col-md-4'.
    // @see setStyles()
    [
      $element,
      $classes,
    ] = explode('.', $this->styles[$rule['style']] . '.', 2);
    $classes = trim(str_replace('.', ' ', $classes));
    foreach ($this->xpath
      ->query($rule['xpath']) as $node) {
      $new_node = $this->document
        ->createElement($element);
      foreach ($node->childNodes as $child) {
        $new_node
          ->appendChild($child
          ->cloneNode(TRUE));
      }
      if ($classes) {
        $new_node
          ->setAttribute('class', $classes);
      }
      $old_node = $node;
      if (!empty($rule['depth'])) {
        for ($i = 0; $i < $rule['depth']; $i++) {
          $old_node = $old_node->parentNode;
        }
      }
      $old_node->parentNode
        ->replaceChild($new_node, $old_node);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DomApplyStyles::$configFactory protected property The config factory.
DomApplyStyles::$styles protected property Array of styles from the WYSIWYG editor.
DomApplyStyles::apply protected function Apply a rule to the document.
DomApplyStyles::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
DomApplyStyles::setStyles protected function Retrieve the list of styles based on configuration.
DomApplyStyles::transform public function Performs the associated process. Overrides ProcessPluginBase::transform
DomApplyStyles::validateRules protected function Validate the configured rules.
DomApplyStyles::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
DomProcessBase::$document protected property Document to use.
DomProcessBase::$xpath protected property Xpath query object.
DomProcessBase::init protected function Initialize the class properties.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.