Version 1
[yaffs-website] / web / modules / contrib / media_entity / src / EmbedCodeValueTrait.php
diff --git a/web/modules/contrib/media_entity/src/EmbedCodeValueTrait.php b/web/modules/contrib/media_entity/src/EmbedCodeValueTrait.php
new file mode 100644 (file)
index 0000000..b9acaca
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+namespace Drupal\media_entity;
+
+use Drupal\Core\Field\FieldItemInterface;
+
+/**
+ * A trait to assist with handling external embed codes.
+ */
+trait EmbedCodeValueTrait {
+
+  /**
+   * Extracts the raw embed code from input which may or may not be wrapped.
+   *
+   * @param mixed $value
+   *   The input value. Can be a normal string or a value wrapped by the
+   *   Typed Data API.
+   *
+   * @return string|null
+   *   The raw embed code.
+   */
+  protected function getEmbedCode($value) {
+    if (is_string($value)) {
+      return $value;
+    }
+    elseif ($value instanceof FieldItemInterface) {
+      $class = get_class($value);
+      $property = $class::mainPropertyName();
+      if ($property) {
+        return $value->$property;
+      }
+    }
+  }
+
+}