Version 1
[yaffs-website] / web / modules / contrib / tocify / src / Plugin / Block / TableOfContents.php
1 <?php
2
3 namespace Drupal\tocify\Plugin\Block;
4
5 use Drupal\Core\Block\BlockBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9 use Drupal\Core\Config\ConfigFactory;
10
11 /**
12  * Provides a 'TableOfContents' block.
13  *
14  * @Block(
15  *  id = "tocify_table_of_contents",
16  *  admin_label = @Translation("Table of contents"),
17  * )
18  */
19 class TableOfContents extends BlockBase implements ContainerFactoryPluginInterface {
20
21   /**
22    * ConfigManager definition.
23    *
24    * @var \Drupal\Core\Config\ConfigFactory
25    */
26   protected $configFactory;
27
28   /**
29    * Construct.
30    *
31    * @param array $configuration
32    *   A configuration array containing information about the plugin instance.
33    * @param string $plugin_id
34    *   The plugin_id for the plugin instance.
35    * @param string $plugin_definition
36    *   The plugin implementation definition.
37    * @param \Drupal\Core\Config\ConfigFactory $config_factory
38    *   The config manager definition.
39    */
40   public function __construct(
41     array $configuration,
42     $plugin_id,
43     $plugin_definition,
44     ConfigFactory $config_factory
45   ) {
46     parent::__construct($configuration, $plugin_id, $plugin_definition);
47     $this->configFactory = $config_factory;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public static function create(
54     ContainerInterface $container,
55     array $configuration,
56     $plugin_id,
57     $plugin_definition
58   ) {
59     return new static(
60       $configuration,
61       $plugin_id,
62       $plugin_definition,
63       $container->get('config.factory')
64     );
65   }
66
67
68   /**
69    * {@inheritdoc}
70    */
71   public function blockForm($form, FormStateInterface $form_state) {
72     $defaults = $this->configFactory
73       ->getEditable('tocify.settings');
74
75     $form['theme'] = array(
76       '#type' => 'textfield',
77       '#title' => $this->t('Theme'),
78       '#description' => $this->t('Choose the theme, e.g. "bootstrap", "jqueryui" or "none"'),
79       '#default_value' => isset($this->configuration['_theme']) ? $this->configuration['_theme'] : $defaults->get('_theme'),
80       '#maxlength' => 64,
81       '#size' => 64,
82       '#weight' => '0',
83     );
84     $form['context'] = array(
85       '#type' => 'textfield',
86       '#title' => $this->t('Context'),
87       '#description' => $this->t('Choose any valid jQuery selector, e.g. "body"'),
88       '#default_value' => isset($this->configuration['_context']) ? $this->configuration['_context'] : $defaults->get('_context'),
89       '#maxlength' => 64,
90       '#size' => 64,
91       '#weight' => '0',
92     );
93     $form['selectors'] = array(
94       '#type' => 'textfield',
95       '#title' => $this->t('Selectors'),
96       '#description' => $this->t('Each comma separated selector must be a header element, e.g. "h1,h2,h3"'),
97       '#default_value' => isset($this->configuration['selectors']) ? $this->configuration['selectors'] : $defaults->get('selectors'),
98       '#maxlength' => 64,
99       '#size' => 64,
100       '#weight' => '0',
101     );
102     $form['show_and_hide'] = array(
103       '#type' => 'textfield',
104       '#title' => $this->t('Show and hide'),
105       '#description' => $this->t('Should elements be shown and hidden, e.g. "true" or "false"'),
106       '#default_value' => isset($this->configuration['show_and_hide']) ? $this->configuration['show_and_hide'] : $defaults->get('show_and_hide'),
107       '#maxlength' => 64,
108       '#size' => 64,
109       '#weight' => '0',
110     );
111     $form['show_effect'] = array(
112       '#type' => 'textfield',
113       '#title' => $this->t('Show effect'),
114       '#description' => $this->t('Any of the jQuery show effects, e.g. "none", "fadeIn", "show", or "slideDown"'),
115       '#default_value' => isset($this->configuration['show_effect']) ? $this->configuration['show_effect'] : $defaults->get('show_effect'),
116       '#maxlength' => 64,
117       '#size' => 64,
118       '#weight' => '0',
119     );
120     $form['show_effect_speed'] = array(
121       '#type' => 'textfield',
122       '#title' => $this->t('Show effect speed'),
123       '#description' => $this->t('The time duration of the show effect, e.g. "slow", "medium", "fast", or any numeric number (milliseconds)'),
124       '#default_value' => isset($this->configuration['show_effect_speed']) ? $this->configuration['show_effect_speed'] : $defaults->get('show_effect_speed'),
125       '#maxlength' => 64,
126       '#size' => 64,
127       '#weight' => '0',
128     );
129     $form['hide_effect'] = array(
130       '#type' => 'textfield',
131       '#title' => $this->t('Hide effect'),
132       '#description' => $this->t('Any of the jQuery hide effects, e.g. "none", "fadeOut", "hide" or "slideUp"'),
133       '#default_value' => isset($this->configuration['hide_effect']) ? $this->configuration['hide_effect'] : $defaults->get('hide_effect'),
134       '#maxlength' => 64,
135       '#size' => 64,
136       '#weight' => '0',
137     );
138     $form['hide_effect_speed'] = array(
139       '#type' => 'textfield',
140       '#title' => $this->t('Hide effect speed'),
141       '#description' => $this->t('The time duration of the hide effect, e.g. "slow", "medium", "fast", or any numeric number (milliseconds)'),
142       '#default_value' => isset($this->configuration['hide_effect_speed']) ? $this->configuration['hide_effect_speed'] : $defaults->get('hide_effect_speed'),
143       '#maxlength' => 64,
144       '#size' => 64,
145       '#weight' => '0',
146     );
147     $form['smooth_scroll'] = array(
148       '#type' => 'textfield',
149       '#title' => $this->t('Smooth scroll'),
150       '#description' => $this->t('Animates the page scroll when specific table of content items are clicked and the page moves up or down, e.g. "true" or "false"'),
151       '#default_value' => isset($this->configuration['smooth_scroll']) ? $this->configuration['smooth_scroll'] : $defaults->get('smooth_scroll'),
152       '#maxlength' => 64,
153       '#size' => 64,
154       '#weight' => '0',
155     );
156     $form['smooth_scroll_speed'] = array(
157       '#type' => 'textfield',
158       '#title' => $this->t('Smooth scroll speed'),
159       '#description' => $this->t('The time duration of the animation'),
160       '#default_value' => isset($this->configuration['smooth_scroll_speed']) ? $this->configuration['smooth_scroll_speed'] : $defaults->get('smooth_scroll_speed'),
161       '#maxlength' => 64,
162       '#size' => 64,
163       '#weight' => '0',
164     );
165     $form['scroll_to'] = array(
166       '#type' => 'textfield',
167       '#title' => $this->t('Scroll to'),
168       '#description' => $this->t('The amount of space between the top of page and the selected table of contents item after the page has been scrolled'),
169       '#default_value' => isset($this->configuration['scroll_to']) ? $this->configuration['scroll_to'] : $defaults->get('scroll_to'),
170       '#maxlength' => 64,
171       '#size' => 64,
172       '#weight' => '0',
173     );
174     $form['show_and_hide_on_scroll'] = array(
175       '#type' => 'textfield',
176       '#title' => $this->t('Show and hide on scroll'),
177       '#description' => $this->t('Determines if table of content nested items should be shown and hidden while a user scrolls the page'),
178       '#default_value' => isset($this->configuration['show_and_hide_on_scroll']) ? $this->configuration['show_and_hide_on_scroll'] : $defaults->get('show_and_hide_on_scroll'),
179       '#maxlength' => 64,
180       '#size' => 64,
181       '#weight' => '0',
182     );
183     $form['highlight_on_scroll'] = array(
184       '#type' => 'textfield',
185       '#title' => $this->t('Highlight on scroll'),
186       '#description' => $this->t('Determines if table of content nested items should be highlighted while scrolling'),
187       '#default_value' => isset($this->configuration['highlight_on_scroll']) ? $this->configuration['highlight_on_scroll'] : $defaults->get('highlight_on_scroll'),
188       '#maxlength' => 64,
189       '#size' => 64,
190       '#weight' => '0',
191     );
192     $form['highlight_offset'] = array(
193       '#type' => 'textfield',
194       '#title' => $this->t('Highlight offset'),
195       '#description' => $this->t('The offset distance in pixels to trigger the next active table of contents item'),
196       '#default_value' => isset($this->configuration['highlight_offset']) ? $this->configuration['highlight_offset'] : $defaults->get('highlight_offset'),
197       '#maxlength' => 64,
198       '#size' => 64,
199       '#weight' => '0',
200     );
201     $form['extend_page'] = array(
202       '#type' => 'textfield',
203       '#title' => $this->t('Extend page'),
204       '#description' => $this->t('If a user scrolls to the bottom of the page and the page is not tall enough to scroll to the last table of contents item, then the page height is increased'),
205       '#default_value' => isset($this->configuration['extend_page']) ? $this->configuration['extend_page'] : $defaults->get('extend_page'),
206       '#maxlength' => 64,
207       '#size' => 64,
208       '#weight' => '0',
209     );
210     $form['extend_page_offset'] = array(
211       '#type' => 'textfield',
212       '#title' => $this->t('Extend page offset'),
213       '#description' => $this->t('How close to the bottom of the page a user must scroll before the page is extended'),
214       '#default_value' => isset($this->configuration['extend_page_offset']) ? $this->configuration['extend_page_offset'] : $defaults->get('extend_page_offset'),
215       '#maxlength' => 64,
216       '#size' => 64,
217       '#weight' => '0',
218     );
219     $form['history'] = array(
220       '#type' => 'textfield',
221       '#title' => $this->t('History'),
222       '#description' => $this->t('Adds a hash to the page url to maintain history'),
223       '#default_value' => isset($this->configuration['history']) ? $this->configuration['history'] : $defaults->get('history'),
224       '#maxlength' => 64,
225       '#size' => 64,
226       '#weight' => '0',
227     );
228     $form['hash_generator'] = array(
229       '#type' => 'textfield',
230       '#title' => $this->t("Hash generator"),
231       '#description' => $this->t("How the URL hash value get's generated"),
232       '#default_value' => isset($this->configuration['hash_generator']) ? $this->configuration['hash_generator'] : $defaults->get('hash_generator'),
233       '#maxlength' => 64,
234       '#size' => 64,
235       '#weight' => '0',
236     );
237     $form['highlight_default'] = array(
238       '#type' => 'textfield',
239       '#title' => $this->t('Highlight default'),
240       '#description' => $this->t("Set's the first table of content item as active if no other item is active"),
241       '#default_value' => isset($this->configuration['highlight_default']) ? $this->configuration['highlight_default'] : $defaults->get('highlight_default'),
242       '#maxlength' => 64,
243       '#size' => 64,
244       '#weight' => '0',
245     );
246     $form['ignore_selector'] = array(
247       '#type' => 'textfield',
248       '#title' => $this->t('Ignore selector'),
249       '#description' => $this->t('Elements that you do not want to be used to generate the table of contents'),
250       '#default_value' => isset($this->configuration['ignore_selector']) ? $this->configuration['ignore_selector'] : $defaults->get('ignore_selector'),
251       '#maxlength' => 64,
252       '#size' => 64,
253       '#weight' => '0',
254     );
255     $form['scroll_history'] = array(
256       '#type' => 'textfield',
257       '#title' => $this->t('Scroll history'),
258       '#description' => $this->t('Adds a hash to the page URL, to maintain history, when scrolling to a table of contents item'),
259       '#default_value' => isset($this->configuration['scroll_history']) ? $this->configuration['scroll_history'] : $defaults->get('scroll_history'),
260       '#maxlength' => 64,
261       '#size' => 64,
262       '#weight' => '0',
263     );
264     return $form;
265   }
266
267   /**
268    * {@inheritdoc}
269    */
270   public function blockSubmit($form, FormStateInterface $form_state) {
271     $this->configuration['_theme'] = $form_state->getValue('theme');
272     $this->configuration['_context'] = $form_state->getValue('context');
273     $this->configuration['selectors'] = $form_state->getValue('selectors');
274     $this->configuration['show_and_hide'] = $form_state->getValue('show_and_hide');
275     $this->configuration['show_effect'] = $form_state->getValue('show_effect');
276     $this->configuration['show_effect_speed'] = $form_state->getValue('show_effect_speed');
277     $this->configuration['hide_effect'] = $form_state->getValue('hide_effect');
278     $this->configuration['hide_effect_speed'] = $form_state->getValue('hide_effect_speed');
279     $this->configuration['smooth_scroll'] = $form_state->getValue('smooth_scroll');
280     $this->configuration['smooth_scroll_speed'] = $form_state->getValue('smooth_scroll_speed');
281     $this->configuration['scroll_to'] = $form_state->getValue('scroll_to');
282     $this->configuration['show_and_hide_on_scroll'] = $form_state->getValue('show_and_hide_on_scroll');
283     $this->configuration['highlight_on_scroll'] = $form_state->getValue('highlight_on_scroll');
284     $this->configuration['highlight_offset'] = $form_state->getValue('highlight_offset');
285     $this->configuration['extend_page'] = $form_state->getValue('extend_page');
286     $this->configuration['extend_page_offset'] = $form_state->getValue('extend_page_offset');
287     $this->configuration['history'] = $form_state->getValue('history');
288     $this->configuration['hash_generator'] = $form_state->getValue('hash_generator');
289     $this->configuration['highlight_default'] = $form_state->getValue('highlight_default');
290     $this->configuration['ignore_selector'] = $form_state->getValue('ignore_selector');
291     $this->configuration['scroll_history'] = $form_state->getValue('scroll_history');
292   }
293
294   /**
295    * {@inheritdoc}
296    */
297   public function build() {
298     $build = [
299       '#theme' => 'tableofcontents',
300       '#_theme' => $this->configuration['_theme'],
301       '#context' => $this->configuration['_context'],
302       '#selectors' => $this->configuration['selectors'],
303       '#show_and_hide' => $this->formatBoolean($this->configuration['show_and_hide']),
304       '#show_effect' => $this->configuration['show_effect'],
305       '#show_effect_speed' => $this->configuration['show_effect_speed'],
306       '#hide_effect' => $this->configuration['hide_effect'],
307       '#hide_effect_speed' => $this->configuration['hide_effect_speed'],
308       '#smooth_scroll' => $this->formatBoolean($this->configuration['smooth_scroll']),
309       '#smooth_scroll_speed' => $this->configuration['smooth_scroll_speed'],
310       '#scroll_to' => (string) $this->configuration['scroll_to'],
311       '#show_and_hide_on_scroll' => $this->formatBoolean($this->configuration['show_and_hide_on_scroll']),
312       '#highlight_on_scroll' => $this->formatBoolean($this->configuration['highlight_on_scroll']),
313       '#highlight_offset' => (string) $this->configuration['highlight_offset'],
314       '#extend_page' => $this->formatBoolean($this->configuration['extend_page']),
315       '#extend_page_offset' => (string) $this->configuration['extend_page_offset'],
316       '#history' => $this->formatBoolean($this->configuration['history']),
317       '#hash_generator' => $this->configuration['hash_generator'],
318       '#highlight_default' => $this->formatBoolean($this->configuration['highlight_default']),
319       '#ignore_selector' => $this->configuration['ignore_selector'],
320       '#scroll_history' => $this->formatBoolean($this->configuration['scroll_history']),
321       '#attached' => array(
322         'library' => array(
323           'tocify/tocify',
324         ),
325       ),
326     ];
327     return $build;
328   }
329
330   /**
331    * Format a boolean as string.
332    *
333    * @param bool $bool
334    *   A boolean to be reformatted as string.
335    *
336    * @return string
337    *   A string in the form of 'true' or 'false'.
338    */
339   private function formatBoolean($bool) {
340     return $bool ? 'true' : 'false';
341   }
342
343 }