Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 672a046b authored by escuriola's avatar escuriola
Browse files

OEL-1147: Branding footer condition.

parent b7fd9570
No related branches found
No related tags found
1 merge request!81OEL-1147 - Footer
......@@ -16,4 +16,9 @@ settings:
label: 'EC Footer block'
provider: oe_corporate_blocks
label_display: '0'
visibility: { }
visibility:
oe_whitelabel_helper_current_component_library:
id: oe_whitelabel_helper_current_component_library
component_library: ec
negate: false
context_mapping: { }
langcode: en
status: true
dependencies:
module:
- oe_corporate_blocks
theme:
- oe_whitelabel
id: oe_whitelabel_eu_corporate_footer
theme: oe_whitelabel
region: footer
weight: 0
provider: null
plugin: oe_corporate_blocks_eu_footer
settings:
id: oe_corporate_blocks_eu_footer
label: 'EU Footer block'
provider: oe_corporate_blocks
label_display: '0'
visibility:
oe_whitelabel_helper_current_component_library:
id: oe_whitelabel_helper_current_component_library
component_library: eu
negate: false
context_mapping: { }
langcode: en
status: true
dependencies:
module:
- oe_whitelabel_helper
theme:
- oe_whitelabel
id: oe_whitelabel_neutral_footer
theme: oe_whitelabel
region: footer
weight: 0
provider: null
plugin: oe_corporate_blocks_neutral_footer
settings:
id: oe_corporate_blocks_neutral_footer
label: 'Neutral Footer block'
provider: oe_whitelabel_helper
label_display: '0'
visibility:
oe_whitelabel_helper_current_component_library:
id: oe_whitelabel_helper_current_component_library
component_library: neutral
negate: false
context_mapping: { }
......@@ -5,3 +5,8 @@ field.formatter.settings.oe_whitelabel_helper_address_inline:
delimiter:
type: string
label: 'Delimiter for address items.'
condition.plugin.oe_whitelabel_helper_current_component_library:
type: condition.plugin
mapping:
component_library:
type: string
<?php
declare(strict_types = 1);
namespace Drupal\oe_whitelabel_helper\Plugin\Condition;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a configurable 'Current component library' condition.
*
* This condition checks if the current component library is equal to a given
* value.
*
* @Condition(
* id = "oe_whitelabel_helper_current_component_library",
* label = @Translation("Current component library")
* )
*/
class CurrentComponentLibraryCondition extends ConditionPluginBase implements ContainerFactoryPluginInterface {
/**
* The configuration factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The theme manager.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface
*/
protected $themeManager;
/**
* Constructs a CurrentComponentLibraryCondition condition plugin.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory service.
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
* The theme manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, ThemeManagerInterface $theme_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->themeManager = $theme_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory'),
$container->get('theme.manager')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'component_library' => '',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
// We allow for an empty value to be set. By doing that we make sure that
// no settings about this condition is actually saved in block visibility
// settings, unless the user explicitly sets one.
$form['component_library'] = [
'#type' => 'select',
'#title' => $this->t('Component library'),
'#options' => [
'' => $this->t('- Any -'),
'neutral' => $this->t('Neutral'),
'ec' => $this->t('European Commission'),
'eu' => $this->t('European Union'),
],
'#default_value' => $this->configuration['component_library'],
'#description' => t('Choose with which component library this condition should be met.'),
];
return parent::buildConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['component_library'] = $form_state->getValue('component_library');
parent::submitConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function evaluate() {
if (empty($this->configuration['component_library'])) {
return TRUE;
}
$theme_name = $this->themeManager->getActiveTheme()->getName();
$component_library = $this->configFactory->get($theme_name . '.settings')->get('component_library');
return $component_library === $this->configuration['component_library'];
}
/**
* {@inheritdoc}
*/
public function summary() {
if (empty($this->configuration['component_library'])) {
return $this->t('The current component library can be set to anything');
}
if ($this->isNegated()) {
return $this->t('The current component library is not @component_library', ['@component_library' => $this->configuration['component_library']]);
}
return $this->t('The current component library is @component_library', ['@component_library' => $this->configuration['component_library']]);
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
$theme_name = $this->themeManager->getActiveTheme()->getName();
return Cache::mergeTags(['config:' . $theme_name . '.settings'], parent::getCacheTags());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment