You are here

class Regions in Context 8

Same name and namespace in other branches
  1. 8.4 src/Plugin/ContextReaction/Regions.php \Drupal\context\Plugin\ContextReaction\Regions

Provides a content reaction that will let you disable regions.

Plugin annotation


@ContextReaction(
  id = "regions",
  label = @Translation("Regions")
)

Hierarchy

Expanded class hierarchy of Regions

File

src/Plugin/ContextReaction/Regions.php, line 20

Namespace

Drupal\context\Plugin\ContextReaction
View source
class Regions extends ContextReactionPluginBase implements ContainerFactoryPluginInterface {

  /**
   * An array of regions to be disabled with this reaction.
   *
   * @var array
   */
  protected $regions = [];

  /**
   * @var \Drupal\Core\Theme\ThemeManagerInterface
   */
  protected $themeManager;

  /**
   * @var \Drupal\Core\Extension\ThemeHandlerInterface
   */
  protected $themeHandler;

  /**
   * {@inheritdoc}
   */
  function __construct(array $configuration, $pluginId, $pluginDefinition, ThemeManagerInterface $themeManager, ThemeHandlerInterface $themeHandler) {
    parent::__construct($configuration, $pluginId, $pluginDefinition);
    $this->themeManager = $themeManager;
    $this->themeHandler = $themeHandler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
    return new static($configuration, $pluginId, $pluginDefinition, $container
      ->get('theme.manager'), $container
      ->get('theme_handler'));
  }

  /**
   * {@inheritdoc}
   */
  public function summary() {
    return $this
      ->t('Lets you remove regions from selected theme.');
  }

  /**
   * Executes the plugin.
   */
  public function execute() {

    // TODO: Implement execute() method.
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $themes = $this->themeHandler
      ->listInfo();
    $default_theme = $this->themeHandler
      ->getDefault();

    // Build configuration form for each installed theme.
    foreach ($themes as $theme_id => $theme) {
      if ($theme_id == $default_theme) {
        $title = $this
          ->t('Disable Regions in %theme (Default)', [
          '%theme' => $theme->info['name'],
        ]);
      }
      else {
        $title = $this
          ->t('Disable Regions in %theme', [
          '%theme' => $theme->info['name'],
        ]);
      }
      $form[$theme_id] = [
        '#type' => 'details',
        '#title' => $title,
        '#weight' => 5,
        '#open' => FALSE,
      ];

      // Get regions of the theme.
      $regions = $this
        ->getSystemRegionList($theme_id);

      // Get disabled regions.
      $disabled_regions = $this
        ->getDisabledRegions();
      $form[$theme_id]['regions'] = [
        '#type' => 'checkboxes',
        '#options' => $regions,
        '#title' => $this
          ->t('Disable the following'),
        '#default_value' => isset($disabled_regions[$theme_id]) ? $disabled_regions[$theme_id] : [],
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $themes = $form_state
      ->getValues();
    if (is_array($themes)) {
      foreach ($themes as $theme_name => $region) {
        $disabled_regions = array_keys(array_filter($region['regions']));
        if (!empty($disabled_regions)) {
          $configuration['regions'][$theme_name] = $disabled_regions;
          $configuration += $this
            ->getConfiguration();
        }
        else {
          $configuration['regions'][$theme_name] = [];
          $configuration += $this
            ->getConfiguration();
        }
        $this
          ->setConfiguration($configuration);
      }
    }
  }

  /**
   * Wraps system_region_list().
   *
   * @param string $theme
   *   The theme to get a list of regions for.
   *
   * @param string $show
   *   What type of regions that should be returned, defaults to all regions.
   *
   * @return array
   *
   * @todo This could be moved to a service since we use it in a couple of places.
   */
  protected function getSystemRegionList($theme, $show = REGIONS_ALL) {
    return system_region_list($theme, $show);
  }

  /**
   * Get disabled regions.
   */
  protected function getDisabledRegions() {
    $configurations = $this
      ->getConfiguration();
    return isset($configurations['regions']) ? $configurations['regions'] : [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContextReactionPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ContextReactionPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurablePluginInterface::defaultConfiguration 2
ContextReactionPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration 1
ContextReactionPluginBase::getId public function Get the unique ID of this context reaction. Overrides ContextReactionInterface::getId
ContextReactionPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration 1
ContextReactionPluginBase::validateConfigurationForm public function Form validation handler is optional. Overrides PluginFormInterface::validateConfigurationForm
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
Regions::$regions protected property An array of regions to be disabled with this reaction.
Regions::$themeHandler protected property
Regions::$themeManager protected property
Regions::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
Regions::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Regions::execute public function Executes the plugin. Overrides ExecutableInterface::execute
Regions::getDisabledRegions protected function Get disabled regions.
Regions::getSystemRegionList protected function Wraps system_region_list().
Regions::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
Regions::summary public function Provides a human readable summary of the condition's configuration. Overrides ContextReactionInterface::summary
Regions::__construct function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides ContextReactionPluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.