You are here

protected function DomApplyStyles::apply in Migrate Plus 8.5

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.

Parameters

string[] $rule: An array with keys 'xpath', 'style', and (optional) 'depth'.

1 call to DomApplyStyles::apply()
DomApplyStyles::transform in src/Plugin/migrate/process/DomApplyStyles.php
Performs the associated process.

File

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

Class

DomApplyStyles
Apply Editor styles to configured elements.

Namespace

Drupal\migrate_plus\Plugin\migrate\process

Code

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);
  }
}