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小节:

评论 (写第一个评论)