10.6 插件上下文

1龴龲龯龮龭龫龨龤

"龭龫龨Context"龎龋龈龮龲龯龇龆龃龤龂齾齺齶齴齰齾齮齬齩齧龴龨齣齟齛齙齕齔龇龆龮齑齐齟齛齌齬齈龋齇齆齃齀鼿龫齔鼻鼸齐齕齃鼶鼵齟齛鼲鼯龇鼫鼨

鼥鼢龎鼡鼠:"鼘鼕鼓鼒鼎drupal"齔鼢黻黺黹黷龮黴鼯龇鼫齃齔黱黯黮黫黩黨"鼘"黧黣黠齔鼓齟齛齴齐齕齈齆齆鼫點黚黖"鼘"龮黠齔龎龋"鼘"黔龮龭龫龨齔龎龋黠黔龮龭龫龨齃黑鼨

鼓黎黍黌黉齴齔龭龫龨龮齈黆黅黁齃鼯麾麾麺齔齰麸黯麴麲鼓齀鼿鼶齃麰麮齆鼫齀鼿鼨黫麬黮麪麧麣麡麞龎麝晦麙齃齆鼫齔麕麧麒黩黨"龭龫龨"麑齑齬"齀鼿"黔麐黖齔齰麸龮麌麊齃鼨

麆麅麄麀鹼鹻鹸鹶鹲鼕齃鹯鹭鹩鹼鹧鹤鹭齔鹣龯鹟齰鹜鹚鹖鹓麞齴齔鹑齶鹣齈鹍齔鼘鹌齈黍鹉鹇黔龮鹆鹅齀鼿齔鹃鼡鼠黷黔龮鹀點黚鸼鸺鸶鼓齃龭龫龨齔鸵鸲鼘鼲鹼鼯鸶麐鸰鼨

鸮齮龭龫龨鸪鸦龮鼢鸥鸣麒齔鼓鸟鸞齙齕鸚鸗齈龋鸕麮鸒鼯鸎齃齃麐黯龮鸚鸗鼓齀鼿鸋龭齃齔鸇鹉鸇鸃鸁龮鼢鸥.

鼓Drupal 8齴龭龫龨黔龮黧齑鷶鷴鹖麀鷲齔鷮鷪鸶齌齃黌鷨齀鼿齔龎龋齀鼿鷤鷶鷴鷣鷮鷪麅鷢鷞鷜鷛鷘齔鷮鷪鷔鷐麒鹀點黚齀鼿鷍龭龫龨鷋鼲黩黨鸼鸺鷇鷃龯麐鸰鷍鼕鷀齐龨鸶鶿鷋鼨

鼥鼢鶻鶺龭龫龨CacheContext齔黔龮黧麑麰齬鶻鶺鷮鷪齃齀鼿鷍鶻鶺齀鼿龮鷮鷪齀鼿齃鸞鶱鷋齔鶻鶺鷮鷪鼻鶯鸥齀鼿鼲鹼鼕鶮麐鸰齔鶭鶪鶧鶣黔龮鶻鶺鼻鶯龎龋龭龫龨麀鶺鹚鶡鶞鶚鼕鶮齃鶖鶯鼨

2龴鶑黍龭龫龨

鶎齾齔鶑黍龭龫龨鶋麸鹚鼓鸎鶈src/Plugin/Condition龫鼨

龫齕鵸鸞鶋麸鶧鶣黖齈龋NodeType鹯齔齰齃龭龫龨龮node鶧鶪齔麕鼯鼓node鶧鶪鵭鵪鵦齃鹇鵤龫齰鼲齺鵢鼨

core/modules/node/src/Plugin/Condition/NodeType.php黣鵝鼢龫鸒

<?php

namespace Drupal\node\Plugin\Condition;

use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a 'Node Type' condition.
 *
 * @Condition(
 *   id = "node_type",
 *   label = @Translation("Node Bundle"),
 *   context = {
 *     "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
 *   }
 * )
 */
class NodeType extends ConditionPluginBase implements ContainerFactoryPluginInterface {

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

  /**
   * Creates a new NodeType instance.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
   *   The entity storage.
   * @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(EntityStorageInterface $entity_storage, array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityStorage = $entity_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $container->get('entity.manager')->getStorage('node_type'),
      $configuration,
      $plugin_id,
      $plugin_definition
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $options = array();
    $node_types = $this->entityStorage->loadMultiple();
    foreach ($node_types as $type) {
      $options[$type->id()] = $type->label();
    }
    $form['bundles'] = array(
      '#title' => $this->t('Node types'),
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => $this->configuration['bundles'],
    );
    return parent::buildConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['bundles'] = array_filter($form_state->getValue('bundles'));
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function summary() {
    if (count($this->configuration['bundles']) > 1) {
      $bundles = $this->configuration['bundles'];
      $last = array_pop($bundles);
      $bundles = implode(', ', $bundles);
      return $this->t('The node bundle is @bundles or @last', array('@bundles' => $bundles, '@last' => $last));
    }
    $bundle = reset($this->configuration['bundles']);
    return $this->t('The node bundle is @bundle', array('@bundle' => $bundle));
  }

  /**
   * {@inheritdoc}
   */
  public function evaluate() {
    if (empty($this->configuration['bundles']) && !$this->isNegated()) {
      return TRUE;
    }
    $node = $this->getContextValue('node');
    return !empty($this->configuration['bundles'][$node->getType()]);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return array('bundles' => array()) + parent::defaultConfiguration();
  }

}

@Condition鳺鳹鳹鳷鸒

  • id: 鶑黍ID
  • label: 鵦鳶鳳齕龭鳱鳯齃鳫鳧
  • context: 鵪鵦evaluate鳣鳟麀鳞鳝node齃鹯鳚

鳘鼘齃齈鳔鳒鸒

  • required: 黧齆龮鳐黯鳍鳌齔鳉鳇黯true
  • multiple: 黧齆龮鳐鼯鳄龋鳂黍齔鳉鳇黯false
  • description: 龭龫龨齃鳁鲿齔鵦齬UI
  • default_value: 鳉鳇黯false
  • class: 鳉鳇\Drupal\Core\Plugin\ContextDefinition鹯鼨鼢黻黮鲻鲹黮鸼鲶齃鹯齔黮鳍鳌鶧鶣\Drupal\Core\Plugin\Context\ContextDefinitionInterface鲴鲲鼨

鳺龇鸒龭龫龨齆鼫鸮鲰黫鲮鵦default_value鳒鼨

3龴鳘鼘齃齈鳔鳯鵸

鳘鼘齃齈鳔鳯鵸鸮鲰鼓Drupal8鸎鶈龫鲨鲥鹖鸒

jerry@mac:~/Sites/drupal8 > grep "@Condition" * -R
core/modules/block/tests/modules/block_test/src/Plugin/Condition/BaloneySpam.php: * @Condition(
core/modules/block/tests/modules/block_test/src/Plugin/Condition/MissingSchema.php: * @Condition(
core/modules/language/src/Plugin/Condition/Language.php: * @Condition(
core/modules/node/src/Plugin/Condition/NodeType.php: * @Condition(
core/modules/system/src/Plugin/Condition/CurrentThemeCondition.php: * @Condition(
core/modules/system/src/Plugin/Condition/RequestPath.php: * @Condition(
core/modules/system/tests/modules/condition_test/src/Plugin/Condition/ConditionTestDualUser.php: * @Condition(
core/modules/system/tests/modules/condition_test/src/Plugin/Condition/ConditionTestNoExistingType.php: * @Condition(
core/modules/system/tests/modules/condition_test/src/Plugin/Condition/OptionalContextCondition.php: * @Condition(
core/modules/user/src/Plugin/Condition/UserRole.php: * @Condition(
modules/ctools/src/Plugin/Condition/EntityBundle.php: * @Condition(

本书共79小节:

评论 (写第一个评论)