Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / blazy / src / Plugin / views / field / BlazyViewsFieldPluginBase.php
1 <?php
2
3 namespace Drupal\blazy\Plugin\views\field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\field\FieldPluginBase;
7 use Drupal\views\ResultRow;
8 use Drupal\blazy\Dejavu\BlazyDefault;
9 use Drupal\blazy\BlazyManagerInterface;
10 use Drupal\blazy\Dejavu\BlazyEntityTrait;
11 use Drupal\blazy\Dejavu\BlazyVideoTrait;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Defines a base views field plugin to render a preview of supported fields.
16  */
17 abstract class BlazyViewsFieldPluginBase extends FieldPluginBase {
18
19   use BlazyEntityTrait;
20   use BlazyVideoTrait;
21
22   /**
23    * The blazy service manager.
24    *
25    * @var \Drupal\blazy\BlazyManagerInterface
26    */
27   protected $blazyManager;
28
29   /**
30    * Constructs a BlazyViewsFieldPluginBase object.
31    */
32   public function __construct(array $configuration, $plugin_id, $plugin_definition, BlazyManagerInterface $blazy_manager) {
33     parent::__construct($configuration, $plugin_id, $plugin_definition);
34     $this->blazyManager = $blazy_manager;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
41     return new static($configuration, $plugin_id, $plugin_definition, $container->get('blazy.manager'));
42   }
43
44   /**
45    * Returns the blazy admin.
46    */
47   public function blazyAdmin() {
48     return \Drupal::service('blazy.admin');
49   }
50
51   /**
52    * Returns the blazy manager.
53    */
54   public function blazyManager() {
55     return $this->blazyManager;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function defineOptions() {
62     $options = parent::defineOptions();
63
64     foreach ($this->getDefaultValues() as $key => $default) {
65       $options[$key] = ['default' => $default];
66     }
67     return $options;
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
74     $definitions = $this->getScopedFormElements();
75
76     $form += $this->blazyAdmin()->baseForm($definitions);
77
78     foreach ($this->getDefaultValues() as $key => $default) {
79       if (isset($form[$key])) {
80         $form[$key]['#default_value'] = isset($this->options[$key]) ? $this->options[$key] : $default;
81         $form[$key]['#weight'] = 0;
82         if (in_array($key, ['box_style', 'box_media_style'])) {
83           $form[$key]['#empty_option'] = $this->t('- None -');
84         }
85       }
86     }
87
88     if (isset($form['view_mode'])) {
89       $form['view_mode']['#description'] = $this->t('Will fallback to this view mode, else entity label.');
90     }
91     parent::buildOptionsForm($form, $form_state);
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function render(ResultRow $values) {
98     return [];
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function query() {
105     // Do nothing -- to override the parent query.
106   }
107
108   /**
109    * Defines the default values.
110    */
111   public function getDefaultValues() {
112     return [
113       'box_style'       => '',
114       'box_media_style' => '',
115       'image_style'     => '',
116       'media_switch'    => 'media',
117       'ratio'           => 'fluid',
118       'thumbnail_style' => '',
119       'view_mode'       => 'default',
120     ];
121   }
122
123   /**
124    * Merges the settings.
125    */
126   public function mergedViewsSettings() {
127     $settings = [];
128
129     // Only fetch what we already asked for.
130     foreach ($this->getDefaultValues() as $key => $default) {
131       $settings[$key] = isset($this->options[$key]) ? $this->options[$key] : $default;
132     }
133
134     $settings['count'] = count($this->view->result);
135     $settings['current_view_mode'] = $this->view->current_display;
136     $settings['view_name'] = $this->view->storage->id();
137
138     return array_merge(BlazyDefault::entitySettings(), $settings);
139   }
140
141   /**
142    * Defines the scope for the form elements.
143    */
144   public function getScopedFormElements() {
145     return [
146       'settings' => array_filter($this->options),
147       'target_type' => !$this->view->getBaseEntityType() ? '' : $this->view->getBaseEntityType()->id(),
148       'thumbnail_style' => TRUE,
149     ];
150   }
151
152 }