Version 1
[yaffs-website] / web / modules / contrib / slick / slick_ui / src / Controller / SlickListBuilder.php
1 <?php
2
3 namespace Drupal\slick_ui\Controller;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Config\Entity\DraggableListBuilder;
10 use Drupal\Component\Utility\Html;
11 use Drupal\blazy\BlazyGrid;
12 use Drupal\slick\SlickManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Provides a listing of Slick optionsets.
17  */
18 class SlickListBuilder extends DraggableListBuilder {
19
20   /**
21    * The slick manager.
22    *
23    * @var \Drupal\slick\SlickManagerInterface
24    */
25   protected $manager;
26
27   /**
28    * Constructs a new SlickListBuilder object.
29    *
30    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
31    *   The entity type definition.
32    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
33    *   The entity storage class.
34    * @param \Drupal\slick\SlickManagerInterface $manager
35    *   The slick manager.
36    */
37   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, SlickManagerInterface $manager) {
38     parent::__construct($entity_type, $storage);
39     $this->manager = $manager;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
46     return new static(
47       $entity_type,
48       $container->get('entity_type.manager')->getStorage($entity_type->id()),
49       $container->get('slick.manager')
50     );
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function getFormId() {
57     return 'slick_list_form';
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function buildHeader() {
64     $header = array(
65       'label'       => $this->t('Optionset'),
66       'breakpoints' => $this->t('Breakpoints'),
67       'group'       => $this->t('Group'),
68       'lazyload'    => $this->t('Lazyload'),
69       'skin'        => $this->t('Skin'),
70     );
71
72     return $header + parent::buildHeader();
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function buildRow(EntityInterface $entity) {
79     $skins = $this->manager->getSkins()['skins'];
80     $skin  = $entity->getSkin();
81
82     $row['label'] = Html::escape($entity->label());
83     $row['breakpoints']['#markup'] = $entity->getBreakpoints();
84     $row['group']['#markup'] = $entity->getGroup() ?: $this->t('All');
85     $row['lazyload']['#markup'] = $entity->getSetting('lazyLoad') ?: $this->t('None');
86
87     $markup = $skin;
88     if (isset($skins[$skin]['description'])) {
89       // No need to re-translate, as already translated at SlickSkin.php.
90       $markup .= '<br />' . Html::escape($skins[$skin]['description']);
91     }
92
93     $row['skin']['#markup'] = $markup;
94
95     return $row + parent::buildRow($entity);
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function getDefaultOperations(EntityInterface $entity) {
102     $operations = parent::getDefaultOperations($entity);
103
104     if (isset($operations['edit'])) {
105       $operations['edit']['title'] = $this->t('Configure');
106     }
107
108     $operations['duplicate'] = array(
109       'title'  => t('Duplicate'),
110       'weight' => 15,
111       'url'    => $entity->toUrl('duplicate-form'),
112     );
113
114     if ($entity->id() == 'default') {
115       unset($operations['delete'], $operations['edit']);
116     }
117
118     return $operations;
119   }
120
121   /**
122    * Adds some descriptive text to the slick optionsets list.
123    *
124    * @return array
125    *   Renderable array.
126    */
127   public function render() {
128     $manager = $this->manager;
129
130     $build['description'] = [
131       '#markup' => $this->t("<p>Manage the Slick optionsets. Optionsets are Config Entities.</p><p>By default, when this module is enabled, a single optionset is created from configuration. Install Slick example module to speed up by cloning them. Use the Operations column to edit, clone and delete optionsets.<br /><strong>Important!</strong> Avoid overriding Default optionset as it is meant for Default -- checking and cleaning. Use Duplicate instead. Otherwise messes are yours.<br />Slick doesn't need Slick UI to run. It is always safe to uninstall Slick UI once done with optionsets.</p>"),
132     ];
133
134     $availaible_skins = [];
135     $skins = $manager->getSkins()['skins'];
136
137     foreach ($skins as $key => $skin) {
138       $name = isset($skin['name']) ? $skin['name'] : $key;
139       $group = isset($skin['group']) ? Html::escape($skin['group']) : 'None';
140       $provider = isset($skin['provider']) ? Html::escape($skin['provider']) : 'Lory';
141       $description = isset($skin['description']) ? Html::escape($skin['description']) : $this->t('No description');
142
143       $markup = '<h3>' . $this->t('@skin <br><small>Id: @id | Group: @group | Provider: @provider</small>', [
144         '@skin' => $name,
145         '@id' => $key,
146         '@group' => $group,
147         '@provider' => $provider,
148       ]) . '</h3>';
149
150       $markup .= '<p><em>&mdash; ' . $description . '</em></p>';
151
152       $availaible_skins[$key] = [
153         '#markup' => '<div class="messages messages--status">' . $markup . '</div>',
154       ];
155     }
156
157     ksort($availaible_skins);
158     $availaible_skins = ['default' => $availaible_skins['default']] + $availaible_skins;
159
160     $settings = [];
161     $settings['grid'] = 3;
162     $settings['grid_medium'] = 2;
163     $settings['blazy'] = FALSE;
164     $settings['style'] = 'column';
165
166     $header = '<br><hr><h2>' . $this->t('Available skins') . '</h2>';
167     $header .= '<p>' . $this->t('Some skin works best with a specific Optionset, and vice versa. Use matching names if found. Else happy adventure!') . '</p>';
168     $build['skins_header']['#markup'] = $header;
169     $build['skins_header']['#weight'] = 20;
170
171     $build['skins'] = BlazyGrid::build($availaible_skins, $settings);
172     $build['skins']['#weight'] = 21;
173     $build['skins']['#attached'] = $manager->attach($settings);
174     $build['skins']['#attached']['library'][] = 'blazy/admin';
175
176     $build[] = parent::render();
177     return $build;
178   }
179
180   /**
181    * {@inheritdoc}
182    */
183   public function submitForm(array &$form, FormStateInterface $form_state) {
184     parent::submitForm($form, $form_state);
185
186     drupal_set_message($this->t('The optionsets order has been updated.'));
187   }
188
189 }