Commit c8b06042 authored by Manuel Adán's avatar Manuel Adán Committed by Manuel Adán
Browse files

Issue #3016820 by manuel.adan: Link to token browser does not work

parent cbfc9d36
Loading
Loading
Loading
Loading
+75 −62
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Contains \Drupal\token_conditions\Plugin\Condition\TokenMatcher.
 *
 * @todo Is there a way to request a generic ContentEntity context?
 */

namespace Drupal\token_conditions\Plugin\Condition;

use Drupal\Core\Condition\ConditionPluginBase;
@@ -19,7 +12,6 @@ use Drupal\Core\Utility\Token;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Entity;

/**
 * Provides a 'Token Matcher' condition.
@@ -31,13 +23,6 @@ use Drupal\Core\Entity\Entity;
 */
class TokenMatcher extends ConditionPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $entityStorage;

  /**
   * The module handler.
   *
@@ -114,63 +99,72 @@ class TokenMatcher extends ConditionPluginBase implements ContainerFactoryPlugin
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form_id = $form_state->getBuildInfo()['form_id'];

    // @todo How to add multiple conditions on block form?
    // @see
    $form['token_match'] = array(
      '#title' => $this->t('Token String'),
    $form['token_match'] = [
      '#title' => $this->t('Source value'),
      '#type' => 'textfield',
      '#default_value' => $this->configuration['token_match'],
      '#description' => $this->t('Enter token or string with multiple tokens'),
    );
    $form['check_empty'] = array(
      '#description' => $this->t('Enter token or string with multiple tokens to be used as source value in the evaluation.'),
    ];

    if ($this->moduleHandler->moduleExists('token')) {
      $form['tokens'] = [
        '#title' => $this->t('Tokens'),
        '#type' => 'container',
      ];

      $form['tokens']['help'] = [
        '#theme' => 'token_tree_link',
        '#token_types' => $this->getContentTokenTypes(),
        '#global_types' => TRUE,
        '#dialog' => TRUE,
      ];
    }
    else {
      $form['tokens'] = [
        '#markup' => $this->t("Note: You don't have the <a href='@token-url'>Token</a> module installed, so the list of available tokens isn't shown here. You don't have to install <a href='@token-url'>Token</a> to be able to use tokens, but if you have it installed, and enabled, you'll be able to enjoy an interactive tokens browser.", ['@token-url' => 'https://www.drupal.org/project/token']),
        '#weight' => 99,
      ];
    }

    $form['check_empty'] = [
      '#type' => 'checkbox',
      '#title' => t('Check if value is empty'),
      '#description' => t('@todo Add description'),
      '#title' => $this->t('Check if value is empty'),
      '#description' => $this->t('Just evaluates the replaced token as empty. Values such 0 or white spaces evaluates positive.'),
      '#default_value' => $this->configuration['check_empty'],
    );
    $invisible_state = array(
      'invisible' => array(
        ':input[name="visibility[token_matcher][check_empty]"]' => array('checked' => TRUE),
      )
    );
    $form['value_match'] = array(
      '#title' => $this->t('Value String'),
    ];

    $invisible_state = [
      'invisible' => [
        ':input[name="visibility[token_matcher][check_empty]"]' => ['checked' => TRUE],
      ],
    ];

    $form['value_match'] = [
      '#title' => $this->t('Expected value'),
      '#type' => 'textfield',
      '#default_value' => $this->configuration['value_match'],
      '#description' => $this->t('Enter string to check against. This can also contain tokens'),
      '#description' => $this->t('Enter string to check against. This can also contain tokens.'),
      '#states' => $invisible_state,
    );
    $form['use_regex'] = array(
    ];

    $form['use_regex'] = [
      '#type' => 'checkbox',
      '#title' => t('Use regex match'),
      '#description' => t(''),
      '#title' => $this->t('Use regex match'),
      '#default_value' => $this->configuration['use_regex'],
      '#states' => $invisible_state,
    );
    if ($this->moduleHandler->moduleExists('token')) {
      $form['token_tree'] = array(
        '#theme' => 'token_tree',
        '#token_types' => $this->getContentTokenTypes(),
        '#dialog' => FALSE,
        '#weight' => 100,
      );
    }
    ];

    return parent::buildConfigurationForm($form, $form_state);
  }


  /**
   * {@inheritdoc}
   */
  public function summary() {
    return $this->t('@token_match = @value_match',
      array(
    return $this->t('@token_match = @value_match', [
      '@token_match' => $this->configuration['token_match'],
      '@value_match' => $this->configuration['value_match'],
      )
    );
    ]);
  }

  /**
@@ -190,11 +184,19 @@ class TokenMatcher extends ConditionPluginBase implements ContainerFactoryPlugin
    return $token_replaced == $value_replace;
  }

  private function getTokenType(ContentEntityType $entity_type) {
  /**
   * Gets the token type for a content entity.
   *
   * @param \Drupal\Core\Entity\ContentEntityType $entity_type
   *   The entity.
   *
   * @return string
   *   The token type.
   */
  protected function getTokenType(ContentEntityType $entity_type) {
    return $entity_type->get('token type') ? $entity_type->get('token type') : $entity_type->id();
  }


  /**
   * {@inheritdoc}
   */
@@ -212,12 +214,12 @@ class TokenMatcher extends ConditionPluginBase implements ContainerFactoryPlugin
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return array(
    return [
      'token_match' => '',
      'value_match' => '',
      'check_empty' => 0,
      'use_regex' => 0,
    ) + parent::defaultConfiguration();
    ] + parent::defaultConfiguration();
  }

  /**
@@ -238,6 +240,12 @@ class TokenMatcher extends ConditionPluginBase implements ContainerFactoryPlugin
    return $token_data;
  }

  /**
   * Get a list of available content token types.
   *
   * @return array
   *   The available content token types.
   */
  protected function getContentTokenTypes() {
    $token_types = [];
    $allEntities = $this->entityManager->getDefinitions();
@@ -246,18 +254,21 @@ class TokenMatcher extends ConditionPluginBase implements ContainerFactoryPlugin
        $token_types[$entity_type] = $this->getTokenType($entity_type_info);
      }
    }

    return $token_types;
  }

  /**
   * Get Entity by type from Request.
   * Get entity by type from current request.
   *
   * This is a stop gap to until there is better way to get the values from
   * context.
   *
   * @param $entity_type
   * @param string $entity_type
   *   The entity type ID.
   *
   * @return mixed
   * @return array|false
   *   Contextual current entity, FALSE if can't be determined.
   */
  protected function getPseudoContextValue($entity_type) {
    $attributes = $this->requestStack->getCurrentRequest()->attributes;
@@ -267,6 +278,8 @@ class TokenMatcher extends ConditionPluginBase implements ContainerFactoryPlugin
        return $entity_attribute;
      }
    }

    return FALSE;
  }

}