Version 1
[yaffs-website] / web / modules / contrib / block_class / block_class.module
diff --git a/web/modules/contrib/block_class/block_class.module b/web/modules/contrib/block_class/block_class.module
new file mode 100644 (file)
index 0000000..c43fde2
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * @file
+ * Module for adding classes to blocks.
+ */
+
+use Drupal\block\Entity\Block;
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function block_class_form_block_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
+  if (\Drupal::currentUser()->hasPermission('administer block classes')) {
+
+    /** @var \Drupal\block\BlockInterface $block */
+    $block = $form_state->getFormObject()->getEntity();
+
+    // This will automatically be saved in the third party settings.
+    $form['third_party_settings']['#tree'] = TRUE;
+    $form['third_party_settings']['block_class']['classes'] = array(
+      '#type' => 'textfield',
+      '#title' => t('CSS class(es)'),
+      '#description' => t('Customize the styling of this block by adding CSS classes. Separate multiple classes by spaces.'),
+      '#default_value' => $block->getThirdPartySetting('block_class', 'classes'),
+    );
+
+  }
+}
+
+/**
+ * Implements hook_preprocess_HOOK().
+ */
+function block_class_preprocess_block(&$variables) {
+  // Blocks coming from page manager widget does not have id.
+  if (!empty($variables['elements']['#id'])) {
+    $block = Block::load($variables['elements']['#id']);
+    if ($classes = $block->getThirdPartySetting('block_class', 'classes')) {
+      $variables['attributes']['class'][] = $classes;
+    }
+  }
+}