10.3 基于注解的插件

Drupal8龡龞龛龚龙龘龗龕龔龐龌龋龐龊龆龃齿齽齹齸齵

1齲齱齰齮齭

齬齨core/modules/user/src/Plugin/Validation/Constraint/UserNameUnique.php龡鼶鼴鼳鼱齬鼭鼩鼦鼢

/**
 * Checks if a user name is unique on the site.
 *
 * @Constraint(
 *   id = "UserNameUnique",
 *   label = @Translation("User name unique", context = "Validation"),
 * )
 */
class UserNameUnique extends Constraint {
...
}

黿黼黹黷黵龙龘Constraint龗龕龔龆龃黱黰黯龘黬黪鼢

  • id 龗龕ID
  • label 黥黡

2齲黚黗黔龞黒黎龆龃

黍龞龋黚黵黉黈Drupal齸黇黆黒黎龔齨Drupal8黄黂龔黀麽龙麹龋齨麷龘module麶龕龡黹黷龔PHP麯麭齸麪麦麢麠麞麚麶龕麯麭麗黇黆龔黿麓麐麏麌麊麆鼭齵麄龐龊龆龃龔麁鹿龞麠龆龃鹼黪麯麭麗黇黆龔鹺麄黒黇黆黒黎鹹鹶黈齵

3齲龆龃齸鹯鹭

龆龃黒黎鹫黹齸鹧鹦齿鹤鹡鹠鹞黈鹛鹗龔鹖鹒鹐鹎鹍齵

龆龃鹯鹭鹌鼢 http://doctrine-orm.readthedocs.io/en/latest/reference/annotations-reference.html?highlight=annotations

  • id: 龗龕齸鹀鸾鸺龔鸶龋鸴龙ID
  • 鸲鸰黒黎鸮鸪鸩龔鸥龞黒黎齰鸪鸩龔齰鸪鸩麢鸡鸝鸜龌
  • 龌黎齸鹛鹗鸚鸙鼢
    • String: 黬鸔鸑鸚鸙鸲鸰黒黎鸮鸪鸩齭齬"foo"齵齬鸐黬鸔鸑龡黱黰鸮鸪鸩鸌鹿鸈黒黎龙鸅鸮鸪鸩鼴鸄黷龔齬 "The ""On"" Value"齵
    • Numbers: 鸥鷾黒黎鸪鸩齭齬21龔黒黎鸪鸩鷼鷸龃鷷齹黬鸔鸑齵
    • Booleans: 鸥鷾黒黎鸪鸩齭齬TRUE鷭鷪FALSE龔黒黎鸪鸩鷼鷸龃鷷齹黬鸔鸑齵
    • Lists: 鷠鷟鹛鷝龔齬:

      base = { "node", "foo", } 鷙龆鷗鷠鷟龡齸鹹鷓龙龘鷐鷌鷓齸鷈鸩龔鷇麚鷄鷀鶿鶻龃鷷鶹鶶齵

    • Maps: 鹛鹗鶱鶮龔鶫齬鹛鷝齵齬:

      edit = { "editor" = "direct"龔 }

    • 鶧鶦黒黎龌鶥

4齲鶡黹黷龆龃鸚

齨 core/modules/text/src/Plugin/Field/FieldFormatter/TextTrimmedFormatter.php 龡鼶鼴鶠鼱黒黎黵龙龘鶡黹黷齸龆龃鸚FieldFormatter齵

<?php

namespace Drupal\text\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'text_trimmed' formatter.
 *
 * Note: This class also contains the implementations used by the
 * 'text_summary_or_trimmed' formatter.
 *
 * @see \Drupal\text\Field\Formatter\TextSummaryOrTrimmedFormatter
 *
 * @FieldFormatter(
 *   id = "text_trimmed",
 *   label = @Translation("Trimmed"),
 *   field_types = {
 *     "text",
 *     "text_long",
 *     "text_with_summary"
 *   },
 *   quickedit = {
 *     "editor" = "form"
 *   }
 * )
 */
class TextTrimmedFormatter extends FormatterBase {
...
}

5齲齨龗龕鸚鸙龡黒黎龆龃

齬鶊齨龗龕鸚鸙龡黒黎龆龃鶆鶅麁龞鶃鶁DefaultPlguinManager鹖黒黎AnnotatedClassDiscovery.

DefaultPluginManager鹞鵽鵻鹛齸鵷龙龘鵳鹛龋鵯黹龗龕齸鵮鵫鵩鵦龔鹹鷓龙龘鵳鹛龋鵣鵠黹黷齸鶡黹黷龆鵞鸚齵齨黿龘齭鵜龡龔鷼麢齨hello_world/src/Plugin/Field/FieldFormatter鵚鵙龡鵮鵫龗龕齵

use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;

class FormatterPluginManager extends DefaultPluginManager {

  /**
   * Constructs a FormatterPluginManager object.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager) {
    parent::__construct('Plugin/Field/FieldFormatter', $namespaces, $module_handler, 'Drupal\Core\Field\FormatterInterface', 'Drupal\Core\Field\Annotation\FieldFormatter');

    $this->setCacheBackend($cache_backend, 'field_formatter_types_plugins');
    $this->alterInfo('field_formatter_info');
    $this->fieldTypeManager = $field_type_manager;
  }
}

麄龆鵐齸鵏鸺鵌鵉齿鶡鷀鵈鵆龆鵐鵄鸾龔齬FieldBundle:

/**
 * @file
 * Contains Drupal\field\FieldBundle.
 */

namespace Drupal\field;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
 * Field dependency injection container.
 */
class FieldBundle extends Bundle {

  /**
   * Overrides Symfony\Component\HttpKernel\Bundle\Bundle::build().
   */
  public function build(ContainerBuilder $container) {
    // Register the plugin managers for our plugin types with the dependency injection container.
    $container->register('plugin.manager.field.widget', 'Drupal\field\Plugin\Type\Widget\WidgetPluginManager')
      ->addArgument('%container.namespaces%');
    $container->register('plugin.manager.field.formatter', 'Drupal\field\Plugin\Type\Formatter\FormatterPluginManager')
      ->addArgument('%container.namespaces%');
  }

}

6齲齬鶊鴸黎鶡黹黷齸龗龕

鸲鸰齨鸚齸鹞鵽鵻鹛龡龆鵐鵏鸺鵌鵉鼢

$type = new CustomPluginManager(\Drupal::getContainer()->getParameter('container.namespaces'));

本书共79小节:

评论 (写第一个评论)