context-8.x-4.x-dev/context.install
context.install
<?php /** * @file * Install, update and uninstall functions for the context module. */ use Drupal\block\Entity\Block; use Drupal\context\Plugin\ContextReaction\Blocks; /** * Update all blocks reactions. * * Add in the reaction configuration an additional "context_id" property. */ function context_update_8001() { foreach (Drupal::service('context.manager')->getContexts() as $context) { foreach ($context->getReactions() as $reaction) { if ($reaction instanceof Blocks) { foreach ($reaction->getBlocks() as $block) { $config = $block->getConfiguration(); if (!isset($config['context_id'])) { $config['context_id'] = $context->id(); $block->setConfiguration($config); $context->save(); } } } } } } /** * Update all blocks reactions. * * Add in the reaction configuration an additional "css_class" property. */ function context_update_8002() { $context_manager = \Drupal::service('context.manager'); foreach ($context_manager->getContexts() as $context) { foreach ($context->getReactions() as $reaction) { if ($reaction instanceof Blocks) { foreach ($reaction->getBlocks() as $block) { $configuration = $block->getConfiguration(); if (!isset($configuration['css_class'])) { $configuration['css_class'] = ''; // Update the block. $reaction->updateBlock($configuration['uuid'], $configuration); $context->save(); } } } } } } /** * Add default "custom_id" property to all blocks reactions. */ function context_update_8003() { foreach (Drupal::service('context.manager')->getContexts() as $context) { foreach ($context->getReactions() as $reaction) { if ($reaction instanceof Blocks) { foreach ($reaction->getBlocks() as $block) { $config = $block->getConfiguration(); if (!isset($config['custom_id'])) { $config['custom_id'] = preg_replace("/\W+/", "_", $config['id']); $block->setConfiguration($config); $context->save(); } } } } } drupal_flush_all_caches(); } /** * Remove unnecessary block dependencies. */ function context_update_8004() { $blocks = Block::loadMultiple(); foreach ($blocks as $block) { $block_visiblity_settings = $block->getVisibility(); if (empty($block_visiblity_settings)) { continue; } if (isset($block_visiblity_settings['context_all']) && empty($block_visiblity_settings['context_all']['values'])) { $block->setVisibilityConfig('context_all', []); } if (isset($block_visiblity_settings['context']) && empty($block_visiblity_settings['context']['values'])) { $block->setVisibilityConfig('context', []); } if (isset($block_visiblity_settings['view_inclusion']) && empty($block_visiblity_settings['view_inclusion']['view_inclusion'])) { $block->setVisibilityConfig('view_inclusion', []); } if (isset($block_visiblity_settings['request_path_exclusion']) && empty($block_visiblity_settings['request_path_exclusion']['pages'])) { $block->setVisibilityConfig('request_path_exclusion', []); } if (isset($block_visiblity_settings['user_status'])) { $user_status_config = $block_visiblity_settings['user_status']; $settings = array_filter($user_status_config['user_status']); if (empty($settings)) { $block->setVisibilityConfig('user_status', []); } } $block->save(); } }