Created
August 28, 2019 01:48
-
-
Save sdeering/40164dcacb242b75b94f0c9caecd9d42 to your computer and use it in GitHub Desktop.
Adds a new tab in the block settings to control the block visibility based on a custom field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\slq_blocks\Plugin\Condition; | |
use Drupal\Core\Condition\ConditionPluginBase; | |
use Drupal\Core\Form\FormStateInterface; | |
/** | |
* Provides a 'Site Theme' condition. | |
* | |
* @Condition( | |
* id = "site_theme", | |
* label = @Translation("Site Theme"), | |
* context_definitions = { | |
* "node" = @ContextDefinition("entity:node", label = @Translation("Node")) | |
* } | |
* ) | |
*/ | |
class SiteTheme extends ConditionPluginBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function getSiteThemes() { | |
$defaultTheme = ['theme1' => 'Site Theme 1']; | |
$themes = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('node')['field_site_theme']->get('settings')['allowed_values']; | |
$themes = array_merge($defaultTheme, $themes); | |
return $themes; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function getCurrentNodeSiteThemeId() { | |
$node = $this->getContextValue('node'); | |
$defaultThemeId = 'theme1'; | |
if ($node && $node->hasField('field_site_theme') && !$node->field_site_theme->isEmpty()) { | |
return $node->field_site_theme->value; | |
} | |
else { | |
return $defaultThemeId; | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
$siteThemes = $this->getSiteThemes(); | |
$currentValues = $this->configuration['site_themes'] ?? []; | |
$form['site_themes'] = [ | |
'#type' => 'checkboxes', | |
'#title' => $this->t('Select the site themes you want this block to appear on:'), | |
'#default_value' => $currentValues, | |
'#options' => array_map('\Drupal\Component\Utility\Html::escape', $siteThemes), | |
'#description' => $this->t('If you select no values, the condition will evaluate to TRUE for all users.'), | |
]; | |
return parent::buildConfigurationForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function defaultConfiguration() { | |
return [ | |
'site_themes' => [], | |
] + parent::defaultConfiguration(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | |
$this->configuration['site_themes'] = array_filter($form_state->getValue('site_themes')); | |
parent::submitConfigurationForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function summary() { | |
$siteThemes = array_intersect_key($this->getSiteThemes(), $this->configuration['site_themes']); | |
if (count($siteThemes) > 1) { | |
$siteThemes = implode(', ', $siteThemes); | |
} | |
else { | |
$siteThemes = reset($siteThemes); | |
} | |
if (!empty($this->configuration['negate'])) { | |
return $this->t('The site theme is not @siteThemes', ['@siteThemes' => $siteThemes]); | |
} | |
else { | |
return $this->t('The site theme is a member of @siteThemes', ['@siteThemes' => $siteThemes]); | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function evaluate() { | |
if (empty($this->configuration['site_themes']) && !$this->isNegated()) { | |
return TRUE; | |
} | |
$siteThemeId = $this->getCurrentNodeSiteThemeId(); | |
return (bool) array_key_exists($siteThemeId, $this->configuration['site_themes']); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getCacheContexts() { | |
return []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment