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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\migrate_plus\Plugin\migrate\process\DomProcessBase
- class \Drupal\migrate_plus\Plugin\migrate\process\DomApplyStyles implements ContainerFactoryPluginInterface
- class \Drupal\migrate_plus\Plugin\migrate\process\DomProcessBase
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\processView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DomApplyStyles:: |
protected | property | The config factory. | |
DomApplyStyles:: |
protected | property | Array of styles from the WYSIWYG editor. | |
DomApplyStyles:: |
protected | function | Apply a rule to the document. | |
DomApplyStyles:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
DomApplyStyles:: |
protected | function | Retrieve the list of styles based on configuration. | |
DomApplyStyles:: |
public | function |
Performs the associated process. Overrides ProcessPluginBase:: |
|
DomApplyStyles:: |
protected | function | Validate the configured rules. | |
DomApplyStyles:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
DomProcessBase:: |
protected | property | Document to use. | |
DomProcessBase:: |
protected | property | Xpath query object. | |
DomProcessBase:: |
protected | function | Initialize the class properties. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |