Version 1
[yaffs-website] / web / modules / contrib / blazy / src / Plugin / views / style / BlazyViews.php
1 <?php
2
3 namespace Drupal\blazy\Plugin\views\style;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\style\StylePluginBase;
7 use Drupal\blazy\BlazyManagerInterface;
8 use Drupal\blazy\Dejavu\BlazyDefault;
9 use Drupal\blazy\BlazyGrid;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Blazy style plugin.
14  */
15 class BlazyViews extends StylePluginBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   protected $usesRowPlugin = TRUE;
21
22   /**
23    * {@inheritdoc}
24    */
25   protected $usesGrouping = FALSE;
26
27   /**
28    * The blazy manager service.
29    *
30    * @var \Drupal\blazy\BlazyManagerInterface
31    */
32   protected $blazyManager;
33
34   /**
35    * Constructs a BlazyManager object.
36    */
37   public function __construct(array $configuration, $plugin_id, $plugin_definition, BlazyManagerInterface $blazy_manager) {
38     parent::__construct($configuration, $plugin_id, $plugin_definition);
39     $this->blazyManager = $blazy_manager;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
46     return new static($configuration, $plugin_id, $plugin_definition, $container->get('blazy.manager'));
47   }
48
49   /**
50    * Returns the blazy admin.
51    */
52   public function admin() {
53     return \Drupal::service('blazy.admin');
54   }
55
56   /**
57    * Returns the blazy manager.
58    */
59   public function blazyManager() {
60     return $this->blazyManager;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   protected function defineOptions() {
67     $options = [];
68     foreach (BlazyDefault::gridSettings() as $key => $value) {
69       $options[$key] = ['default' => $value];
70     }
71     return $options + parent::defineOptions();
72   }
73
74   /**
75    * Overrides StylePluginBase::buildOptionsForm().
76    */
77   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
78     $definition = [
79       'namespace' => 'blazy',
80       'grid_form' => TRUE,
81       'settings'  => $this->options,
82       'style'     => TRUE,
83       'form_opening_classes' => 'form--blazy form--slick form--views form--half has-tooltip',
84     ];
85
86     // Build the form.
87     $this->admin()->openingForm($form, $definition);
88     $this->admin()->gridForm($form, $definition);
89
90     if (isset($form['grid'])) {
91       $form['grid']['#description'] = $this->t('The amount of block grid columns for large monitors 64.063em.');
92     }
93
94     $this->admin()->finalizeForm($form, $definition);
95
96     // Blazy doesn't need complex grid with multiple groups.
97     unset($form['layout'], $form['preserve_keys'], $form['grid_header'], $form['visible_items'], $form['style']['#empty_option'], $form['grid']['#empty_option']);
98   }
99
100   /**
101    * Overrides StylePluginBase::render().
102    */
103   public function render() {
104     $settings = $this->options;
105
106     $settings['count']             = count($this->view->result);
107     $settings['current_view_mode'] = $this->view->current_display;
108     $settings['item_id']           = 'content';
109     $settings['namespace']         = 'blazy';
110     $settings['view_name']         = $this->view->storage->id();
111
112     $elements = [];
113     foreach ($this->renderGrouping($this->view->result, $settings['grouping']) as $rows) {
114       $items = [];
115       foreach ($rows as $index => $row) {
116         $this->view->row_index = $index;
117
118         $items[$index] = $this->view->rowPlugin->render($row);
119       }
120
121       // Supports Blazy formatter multi-breakpoint images if available.
122       $this->blazyManager->isBlazy($settings, $items[0]);
123       $elements = BlazyGrid::build($items, $settings);
124       $elements['#attached'] = $this->blazyManager->attach($settings);
125
126       unset($this->view->row_index, $items);
127     }
128
129     return $elements;
130   }
131
132 }