diff modules/contrib/migrate_plus/src/Plugin/migrate/process/DomProcessBase.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/contrib/migrate_plus/src/Plugin/migrate/process/DomProcessBase.php	Thu May 09 15:34:47 2019 +0100
@@ -0,0 +1,56 @@
+<?php
+
+namespace Drupal\migrate_plus\Plugin\migrate\process;
+
+use Drupal\migrate\MigrateSkipRowException;
+use Drupal\migrate\ProcessPluginBase;
+
+/**
+ * Base class for process plugins that work with \DOMDocument objects.
+ *
+ * Use Dom::import() to convert a string to a \DOMDocument object, then plugins
+ * derived from this class to manipulate the object, then Dom::export() to
+ * convert back to a string.
+ */
+abstract class DomProcessBase extends ProcessPluginBase {
+
+  /**
+   * Document to use.
+   *
+   * @var \DOMDocument
+   */
+  protected $document;
+
+  /**
+   * Xpath query object.
+   *
+   * @var \DOMXPath
+   */
+  protected $xpath;
+
+  /**
+   * Initialize the class properties.
+   *
+   * @param mixed $value
+   *   Process plugin value.
+   * @param string $destination_property
+   *   The name of the destination being processed. Used to generate an error
+   *   message.
+   *
+   * @throws \Drupal\migrate\MigrateSkipRowException
+   *   If $value is not a \DOMDocument object.
+   */
+  protected function init($value, $destination_property) {
+    if (!($value instanceof \DOMDocument)) {
+      $message = sprintf(
+        'The %s plugin in the %s process pipeline requires a \DOMDocument object. You can use the dom plugin to convert a string to \DOMDocument.',
+        $this->getPluginId(),
+        $destination_property
+      );
+      throw new MigrateSkipRowException($message);
+    }
+    $this->document = $value;
+    $this->xpath = new \DOMXPath($this->document);
+  }
+
+}