|
@@ -0,0 +1,101 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Drupal\paragraphs_views\Plugin\Field\FieldFormatter;
|
|
|
+
|
|
|
+use Drupal\Component\Utility\Html;
|
|
|
+use Drupal\Core\Field\FieldItemInterface;
|
|
|
+use Drupal\Core\Field\FieldItemListInterface;
|
|
|
+use Drupal\Core\Field\FormatterBase;
|
|
|
+use Drupal\Core\Form\FormStateInterface;
|
|
|
+use Drupal\views\Element\View;
|
|
|
+use Drupal\views\Views;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Plugin implementation of the 'paragraphs_views_formatter' formatter.
|
|
|
+ *
|
|
|
+ * @FieldFormatter(
|
|
|
+ * id = "paragraphs_views_formatter",
|
|
|
+ * label = @Translation("Views formatter"),
|
|
|
+ * field_types = {
|
|
|
+ * "entity_reference"
|
|
|
+ * }
|
|
|
+ * )
|
|
|
+ */
|
|
|
+class ViewsFormatter extends FormatterBase {
|
|
|
+ const DISPLAY = 'display';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * {@inheritdoc}
|
|
|
+ */
|
|
|
+ public static function defaultSettings() {
|
|
|
+ return array(
|
|
|
+ 'display' => NULL,
|
|
|
+ ) + parent::defaultSettings();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * {@inheritdoc}
|
|
|
+ */
|
|
|
+ public function settingsForm(array $form, FormStateInterface $form_state) {
|
|
|
+ $elements = [];
|
|
|
+ $elements['display'] = array(
|
|
|
+ '#type' => 'textfield',
|
|
|
+ '#title' => t('Name of View display'),
|
|
|
+ '#default_value' => $this->getSetting('display'),
|
|
|
+ '#description' => t('The machine name of the display to use for when executing the view'),
|
|
|
+ '#required' => FALSE
|
|
|
+ );
|
|
|
+
|
|
|
+ return $elements + parent::settingsForm($form, $form_state);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * {@inheritdoc}
|
|
|
+ */
|
|
|
+ public function settingsSummary() {
|
|
|
+ $summary = [];
|
|
|
+ $display = $this->getSetting('display');
|
|
|
+ $summary[] = t('Display: @display', array('@display' => $display ?: t('(default)')));
|
|
|
+
|
|
|
+ return $summary;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * {@inheritdoc}
|
|
|
+ */
|
|
|
+ public function viewElements(FieldItemListInterface $items, $langcode) {
|
|
|
+ $elements = [];
|
|
|
+ $display = $this->getSetting(static::DISPLAY) ?? 'default';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item
|
|
|
+ */
|
|
|
+ foreach ($items as $delta => $item) {
|
|
|
+ $target = $item->getValue()['target_id'];
|
|
|
+ $view = \Drupal\views\Entity\View::load($target);
|
|
|
+ $exec = $view->getExecutable();
|
|
|
+ $elements[$delta] = ['view' => $exec->preview($display)];
|
|
|
+ }
|
|
|
+
|
|
|
+ $elements['#cache'] = [
|
|
|
+ 'max-age' => 0,
|
|
|
+ ];
|
|
|
+ return $elements;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Generate the output appropriate for one field item.
|
|
|
+ *
|
|
|
+ * @param \Drupal\Core\Field\FieldItemInterface $item
|
|
|
+ * One field item.
|
|
|
+ *
|
|
|
+ * @return string
|
|
|
+ * The textual output generated.
|
|
|
+ */
|
|
|
+ protected function viewValue(FieldItemInterface $item) {
|
|
|
+ // The text value has no text format assigned to it, so the user input
|
|
|
+ // should equal the output, including newlines.
|
|
|
+ return nl2br(Html::escape($item->value));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|