0

I have the following condition that check if a node has field_tags to return true

    <?php
    namespace Drupal\custom_conditions\Plugin\Condition;
    use Drupal\Core\Condition\ConditionPluginBase;
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    /**
     * Provides the 'SpecificTag condition' condition.
     *
     * @Condition(
     *   id = "specific_tag_condition",
     *   label = @Translation("SpecificTag block condition"),
     *   context = {
     *     "node" = @ContextDefinition(
     *        "entity:node",
     *        required = TRUE ,
     *        label = @Translation("node")
     *     )
     *   }
     * )
     */
    class SpecificTagCondition 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['SpecificTagActive'] = [
          '#type'          => 'checkbox',
          '#title'         => $this->t('When SpecificTag Active'),
          '#default_value' => $this->configuration['SpecificTagActive'],
          '#description'   => $this->t('Enable this block when the SpecificTag is present.'),
        ];
        return parent::buildConfigurationForm($form, $form_state);
      }
      /**
       * {@inheritdoc}
       */
      public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
        $this->configuration['SpecificTagActive'] = $form_state->getValue('SpecificTagActive');
        parent::submitConfigurationForm($form, $form_state);
      }
      /**
       * {@inheritdoc}
       */
      public function defaultConfiguration() {
          return ['SpecificTagActive' => 0] + parent::defaultConfiguration();
      }
      /**
       * Provides a human readable summary of the condition's configuration.
       */
      public function summary() {
        $status = $this->configuration['SpecificTagActive'] ? t('enabled') : t('disabled');
        return t(
          'The node has SpecificTag @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['SpecificTagActive']) && !$this->isNegated()) {
          return TRUE;
        }
        $node = $this->getContextValue('node');
        if ($node->hasField('field_tags')) {
          return TRUE;
        }
        return FALSE;
      }
    }

I can actually see this condition and select it and enable it but evaluate seems its never called when visiting a node that has tags field. Debugging it seems that only defaultConfiguration is called. Any pointers with my first condition ? what am I doing wrong ?

EDIT: It turns out that rules module creates some trouble with its own conditions so after disabling rules the condition worked.

  • It looks fine imho... how are you checking that evaluate isn't called? (Also btw you can use $this->t() for translations, it's available by default in anything extending ContextAwarePluginBase.) – Andy Jun 14 '18 at 12:21
  • @Andy that is what I thought, I am using netbeans with xdebug and the evaluate is never called even after several attempts and clear cache etc ... perhaps its something in my installation ... – GiorgosK Jun 14 '18 at 12:22
  • Just to be super sure you could try putting a die('got this far'); at the start of the evaluate (to rule out issues with xdebug, etc.). – Andy Jun 14 '18 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.