Browse Source

First commit: skeleton in place.

Frederic G. MARAND 7 years ago
commit
25afeb999c

+ 14 - 0
composer.json

@@ -0,0 +1,14 @@
+{
+  "name": "drupal/paragraphs_views",
+  "type": "drupal-module",
+  "description": "Use views as paragraphs",
+  "keywords": ["Drupal"],
+  "license": "GPL-2.0+",
+  "homepage": "https://www.drupal.org/project/paragraphs_views",
+  "minimum-stability": "dev",
+  "support": {
+    "issues": "http://drupal.org/project/issues/paragraphs_views",
+    "source": "http://cgit.drupalcode.org/paragraphs_views"
+  },
+  "require": { }
+}

+ 8 - 0
paragraphs_views.info.yml

@@ -0,0 +1,8 @@
+name: Paragraphs Views
+type: module
+description: Use views as paragraphs
+core: 8.x
+package: Paragraphs
+dependencies:
+  - paragraphs
+  - views

+ 24 - 0
paragraphs_views.module

@@ -0,0 +1,24 @@
+<?php
+
+/**
+ * @file
+ * Contains paragraphs_views.module..
+ */
+
+use Drupal\Core\Routing\RouteMatchInterface;
+
+/**
+ * Implements hook_help().
+ */
+function paragraphs_views_help($route_name, RouteMatchInterface $route_match) {
+  switch ($route_name) {
+    // Main module help for the paragraphs_views module.
+    case 'help.page.paragraphs_views':
+      $output = '';
+      $output .= '<h3>' . t('About') . '</h3>';
+      $output .= '<p>' . t('Use views as paragraphs') . '</p>';
+      return $output;
+
+    default:
+  }
+}

+ 101 - 0
src/Plugin/Field/FieldFormatter/ViewsFormatter.php

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

+ 85 - 0
src/Plugin/Field/FieldWidget/ViewsWidget.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace Drupal\paragraphs_views\Plugin\Field\FieldWidget;
+
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\Core\Field\WidgetBase;
+use Drupal\Core\Form\FormStateInterface;
+
+/**
+ * Plugin implementation of the 'paragraphs_view_widget' widget.
+ *
+ * @FieldWidget(
+ *   id = "paragraphs_view_widget",
+ *   label = @Translation("Views widget"),
+ *   field_types = {
+ *     "entity_reference"
+ *   }
+ * )
+ */
+class ViewsWidget extends WidgetBase {
+  /**
+   * {@inheritdoc}
+   */
+  public static function defaultSettings() {
+    return array(
+      'size' => 60,
+      'placeholder' => '',
+    ) + parent::defaultSettings();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function settingsForm(array $form, FormStateInterface $form_state) {
+    $elements = [];
+
+    $elements['size'] = array(
+      '#type' => 'number',
+      '#title' => t('Size of textfield'),
+      '#default_value' => $this->getSetting('size'),
+      '#required' => TRUE,
+      '#min' => 1,
+    );
+    $elements['placeholder'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Placeholder'),
+      '#default_value' => $this->getSetting('placeholder'),
+      '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
+    );
+
+    return $elements;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function settingsSummary() {
+    $summary = [];
+
+    $summary[] = t('Textfield size: !size', array('!size' => $this->getSetting('size')));
+    if (!empty($this->getSetting('placeholder'))) {
+      $summary[] = t('Placeholder: @placeholder', array('@placeholder' => $this->getSetting('placeholder')));
+    }
+
+    return $summary;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
+    $element = [];
+
+    $element['value'] = $element + array(
+      '#type' => 'textfield',
+      '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
+      '#size' => $this->getSetting('size'),
+      '#placeholder' => $this->getSetting('placeholder'),
+      '#maxlength' => $this->getFieldSetting('max_length'),
+    );
+
+    return $element;
+  }
+
+}