You are here

block_style_plugins.module in Block Style Plugins 8

Same filename and directory in other branches
  1. 8.2 block_style_plugins.module

File

block_style_plugins.module
View source
<?php

/**
 * @file
 * Contains block_style_plugins.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 */
function block_style_plugins_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the block_style_plugins module.
    case 'help.page.block_style_plugins':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Plugin system for allowing modules or themes to add style configuration to blocks') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * For the ID form_block.
 */
function block_style_plugins_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Retrieve a list of style plugin definitions.

  /** @var Drupal\block_style_plugins\Plugin\BlockStyleManager $plugin_manager */
  $plugin_manager = \Drupal::service('plugin.manager.block_style.processor');
  $style_plugins = $plugin_manager
    ->getDefinitions();
  foreach ($style_plugins as $name => $plugin) {

    /** @var \Drupal\block_style_plugins\Plugin\BlockStyleInterface $style_plugin */
    $style_plugin = $plugin_manager
      ->createInstance($name);
    $form = $style_plugin
      ->prepareForm($form, $form_state);
  }
}

/**
 * Implements hook_preprocess_block().
 */
function block_style_plugins_preprocess_block(&$variables) {

  // Retrieve a list of style plugin definitions.

  /** @var Drupal\block_style_plugins\Plugin\BlockStyleManager $plugin_manager */
  $plugin_manager = \Drupal::service('plugin.manager.block_style.processor');
  $style_plugins = $plugin_manager
    ->getDefinitions();
  foreach ($style_plugins as $name => $plugin) {
    $variables = $plugin_manager
      ->createInstance($name)
      ->build($variables);
  }
}

/**
 * Implements hook_theme_suggestions_block_alter().
 */
function block_style_plugins_theme_suggestions_block_alter(array &$suggestions, array $variables) {

  // Retrieve a list of style plugin definitions.

  /** @var Drupal\block_style_plugins\Plugin\BlockStyleManager $plugin_manager */
  $plugin_manager = \Drupal::service('plugin.manager.block_style.processor');
  $style_plugins = $plugin_manager
    ->getDefinitions();
  foreach ($style_plugins as $name => $plugin) {
    $suggestions = $plugin_manager
      ->createInstance($name)
      ->themeSuggestion($suggestions, $variables);
  }
}

/**
 * Implements hook_themes_uninstalled().
 */
function block_style_plugins_themes_uninstalled(array $themes) {

  // Clear the Plugins cache when a theme using a plugin is uninstalled.

  /** @var Drupal\block_style_plugins\Plugin\BlockStyleManager $plugin_manager */
  $plugin_manager = \Drupal::service('plugin.manager.block_style.processor');
  $style_plugins = $plugin_manager
    ->getDefinitions();
  $clear_cache = FALSE;
  foreach ($themes as $theme_name) {
    foreach ($style_plugins as $plugin) {
      if ($plugin['provider'] == $theme_name) {
        $clear_cache = TRUE;
        break;
      }
    }
  }

  // Clear all plugin caches.
  if ($clear_cache) {
    \Drupal::service('plugin.cache_clearer')
      ->clearCachedDefinitions();
  }
}