Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / paragraphs / src / Plugin / Field / FieldFormatter / ParagraphsSummaryFormatter.php
diff --git a/web/modules/contrib/paragraphs/src/Plugin/Field/FieldFormatter/ParagraphsSummaryFormatter.php b/web/modules/contrib/paragraphs/src/Plugin/Field/FieldFormatter/ParagraphsSummaryFormatter.php
new file mode 100644 (file)
index 0000000..2416f02
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+
+namespace Drupal\paragraphs\Plugin\Field\FieldFormatter;
+
+use Drupal\Core\Field\FieldDefinitionInterface;
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
+use Drupal\paragraphs\Entity\Paragraph;
+use Drupal\paragraphs\ParagraphInterface;
+
+/**
+ * Plugin implementation of the 'paragraph_summary' formatter.
+ *
+ * @FieldFormatter(
+ *   id = "paragraph_summary",
+ *   label = @Translation("Paragraph summary"),
+ *   field_types = {
+ *     "entity_reference_revisions"
+ *   }
+ * )
+ */
+class ParagraphsSummaryFormatter extends EntityReferenceFormatterBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function viewElements(FieldItemListInterface $items, $langcode) {
+    $elements = [];
+    foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
+      if ($entity->id()) {
+        $elements[$delta] = [
+          '#markup' => $entity->getSummary(),
+        ];
+      }
+    }
+
+    return $elements;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function isApplicable(FieldDefinitionInterface $field_definition) {
+    $target_type = $field_definition->getSetting('target_type');
+    $paragraph_type = \Drupal::entityTypeManager()->getDefinition($target_type);
+    if ($paragraph_type) {
+      return $paragraph_type->isSubclassOf(ParagraphInterface::class);
+    }
+
+    return FALSE;
+  }
+}