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

评论 (写第一个评论)