Skip to content

Instantly share code, notes, and snippets.

@acrosman
Last active December 1, 2021 01:50
Show Gist options
  • Save acrosman/ab7e9ffce3996f6bc1798f9f1ca34c06 to your computer and use it in GitHub Desktop.
Save acrosman/ab7e9ffce3996f6bc1798f9f1ca34c06 to your computer and use it in GitHub Desktop.
A Drupal 8 block condition to set visibility based on a node's field value.
<?php
namespace Drupal\my_block\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the 'Sidebar condition' condition.
*
* @Condition(
* id = "sidebar_condition",
* label = @Translation("Sidebar block condition"),
* context_definitions = {
* "node" = @ContextDefinition(
* "entity:node",
* required = TRUE ,
* label = @Translation("node")
* )
* }
* )
*/
class SidebarCondition extends ConditionPluginBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition
);
}
/**
* Creates a new SidebarCondition object.
*
* @param array $configuration
* The plugin configuration, i.e. an array with configuration values keyed
* by configuration option name. The special key 'context' may be used to
* initialize the defined contexts by setting it to an array of context
* values keyed by context names.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['sidebarActive'] = [
'#type' => 'checkbox',
'#title' => $this->t('When Sidebar Field Active'),
'#default_value' => $this->configuration['sidebarActive'],
'#description' => $this->t('Enable this block when the sidebar field on the node is active.'),
];
return parent::buildConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['sidebarActive'] = $form_state->getValue('sidebarActive');
parent::submitConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return ['sidebarActive' => 0] + parent::defaultConfiguration();
}
/**
* Provides a human readable summary of the condition's configuration.
*/
public function summary() {
$status = $this->getContextValue('sidebarActive') ? t('enabled') : t('disabled');
return t(
'The node has sidebar block @status.',
['@status' => $status]);
}
/**
* Evaluates the condition and returns TRUE or FALSE accordingly.
*
* @return bool
* TRUE if the condition has been met, FALSE otherwise.
*/
public function evaluate() {
if (empty($this->configuration['sidebarActive']) && !$this->isNegated()) {
return TRUE;
}
$node = $this->getContextValue('node');
if ($node->hasField('field_enable_sidebar') && $node->field_enable_sidebar->value) {
return TRUE;
}
return FALSE;
}
}
@ahebrank
Copy link

Thanks for the article and gist. I think https://gist.github.com/acrosman/ab7e9ffce3996f6bc1798f9f1ca34c06#file-sidebarcondition-php-L89 should be:

$status = $this->configuration['sidebarActive'] ? t('enabled') : t('disabled');

@pricejn2
Copy link

pricejn2 commented Jul 9, 2020

Adding a link back to your article https://spinningcode.org/2017/06/controlling-block-visibility-with-a-custom-field-in-drupal-8/ since I found the gist first

@pcambra
Copy link

pcambra commented Nov 30, 2021

Context is now context_definitions:

 *   context_definitions = {
 *     "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
 *   }

@acrosman
Copy link
Author

acrosman commented Dec 1, 2021

Oh thanks for catching that. For anyone interested the change record is here: https://www.drupal.org/node/3016699

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment