Commit 984940b8 authored by alexpott's avatar alexpott

Issue #1985344 by tim.plunkett, swentel: Add a dedicated @FieldFormatter annotation.

parent 95368fca
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\datetime\Plugin\field\formatter; namespace Drupal\datetime\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
/** /**
* Plugin implementation of the 'datetime_default' formatter. * Plugin implementation of the 'datetime_default' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "datetime_default", * id = "datetime_default",
* module = "datetime", * module = "datetime",
* label = @Translation("Default"), * label = @Translation("Default"),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\datetime\Plugin\field\formatter; namespace Drupal\datetime\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
/** /**
* Plugin implementation of the 'datetime_plain' formatter. * Plugin implementation of the 'datetime_plain' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "datetime_plain", * id = "datetime_plain",
* module = "datetime", * module = "datetime",
* label = @Translation("Plain"), * label = @Translation("Plain"),
......
...@@ -133,6 +133,23 @@ function edit_library_info() { ...@@ -133,6 +133,23 @@ function edit_library_info() {
return $libraries; return $libraries;
} }
/**
* Implements hook_field_formatter_info_alter().
*
* Edit extends the @FieldFormatter annotation with the following keys:
* - edit: currently only contains one subkey 'editor' which indicates which
* in-place editor should be used. Possible values are 'form', 'direct' or
* 'disabled'.
*/
function edit_field_formatter_info_alter(&$info) {
foreach ($info as $key => $settings) {
// Set in-place editor to form if none is supplied.
if (empty($settings['edit'])) {
$info[$key]['edit'] = array('editor' => 'form');
}
}
}
/** /**
* Implements hook_preprocess_HOOK() for field.tpl.php. * Implements hook_preprocess_HOOK() for field.tpl.php.
*/ */
......
...@@ -63,7 +63,7 @@ public function getEditor($formatter_type, FieldInstance $instance, array $items ...@@ -63,7 +63,7 @@ public function getEditor($formatter_type, FieldInstance $instance, array $items
// 'form' editor, since that can work for any field. Formatter definitions // 'form' editor, since that can work for any field. Formatter definitions
// can use 'disabled' to explicitly opt out of in-place editing. // can use 'disabled' to explicitly opt out of in-place editing.
$formatter_info = field_info_formatter_types($formatter_type); $formatter_info = field_info_formatter_types($formatter_type);
$editor_id = isset($formatter_info['edit']['editor']) ? $formatter_info['edit']['editor'] : 'form'; $editor_id = $formatter_info['edit']['editor'];
if ($editor_id === 'disabled') { if ($editor_id === 'disabled') {
return; return;
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\email\Plugin\field\formatter; namespace Drupal\email\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
/** /**
* Plugin implementation of the 'email_mailto' formatter. * Plugin implementation of the 'email_mailto' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "email_mailto", * id = "email_mailto",
* module = "email", * module = "email",
* label = @Translation("Email"), * label = @Translation("Email"),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\entity_reference\Plugin\field\formatter; namespace Drupal\entity_reference\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\entity_reference\RecursiveRenderingException; use Drupal\entity_reference\RecursiveRenderingException;
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
/** /**
* Plugin implementation of the 'entity reference rendered entity' formatter. * Plugin implementation of the 'entity reference rendered entity' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "entity_reference_entity_view", * id = "entity_reference_entity_view",
* module = "entity_reference", * module = "entity_reference",
* label = @Translation("Rendered entity"), * label = @Translation("Rendered entity"),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\entity_reference\Plugin\field\formatter; namespace Drupal\entity_reference\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\field\Plugin\Type\Formatter\FormatterBase;
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\entity_reference\Plugin\field\formatter; namespace Drupal\entity_reference\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase; use Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase;
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
/** /**
* Plugin implementation of the 'entity reference ID' formatter. * Plugin implementation of the 'entity reference ID' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "entity_reference_entity_id", * id = "entity_reference_entity_id",
* module = "entity_reference", * module = "entity_reference",
* label = @Translation("Entity ID"), * label = @Translation("Entity ID"),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\entity_reference\Plugin\field\formatter; namespace Drupal\entity_reference\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase; use Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase;
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
/** /**
* Plugin implementation of the 'entity reference label' formatter. * Plugin implementation of the 'entity reference label' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "entity_reference_label", * id = "entity_reference_label",
* module = "entity_reference", * module = "entity_reference",
* label = @Translation("Label"), * label = @Translation("Label"),
......
<?php
/**
* @file
* Contains \Drupal\field\Annotation\FieldFormatter.
*/
namespace Drupal\field\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a FieldFormatter annotation object.
*
* Formatters handle the display of field values. Formatter hooks are typically
* called by the Field Attach API field_attach_prepare_view() and
* field_attach_view() functions.
*
* Additional annotation keys for formatters can be defined in
* hook_field_formatter_info_alter().
*
* @Annotation
*
* @see \Drupal\field\Plugin\Type\Formatter\FormatterPluginManager
* @see \Drupal\field\Plugin\Type\Formatter\FormatterInterface
*/
class FieldFormatter extends Plugin {
/**
* The plugin ID.
*
* @var string
*/
public $id;
/**
* The human-readable name of the formatter type.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $label;
/**
* A short description of the formatter type.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $description;
/**
* The name of the module providing the formatter.
*
* @var string
*/
public $module;
/**
* The name of the field formatter class.
*
* This is not provided manually, it will be added by the discovery mechanism.
*
* @var string
*/
public $class;
/**
* An array of field types the formatter supports.
*
* @var array
*/
public $field_types = array();
/**
* An array whose keys are the names of the settings available to the
* formatter type, and whose values are the default values for those settings.
*
* @var array
*/
public $settings = array();
}
<?php
/**
* @file
* Definition of Drupal\field\Plugin\Type\Formatter\FormatterFactory.
*/
namespace Drupal\field\Plugin\Type\Formatter;
use Drupal\Component\Plugin\Factory\DefaultFactory;
/**
* Factory class for the Formatter plugin type.
*/
class FormatterFactory extends DefaultFactory {
/**
* Overrides Drupal\Component\Plugin\Factory\DefaultFactory::createInstance().
*/
public function createInstance($plugin_id, array $configuration) {
$plugin_definition = $this->discovery->getDefinition($plugin_id);
$plugin_class = static::getPluginClass($plugin_id, $plugin_definition);
return new $plugin_class($plugin_id, $plugin_definition, $configuration['instance'], $configuration['settings'], $configuration['label'], $configuration['view_mode']);
}
}
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
namespace Drupal\field\Plugin\Type\Formatter; namespace Drupal\field\Plugin\Type\Formatter;
use Drupal\Component\Plugin\PluginManagerBase; use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Discovery\ProcessDecorator; use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Plugin\Discovery\CacheDecorator; use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\AlterDecorator; use Drupal\Core\Plugin\Discovery\AlterDecorator;
...@@ -26,14 +26,6 @@ class FormatterPluginManager extends PluginManagerBase { ...@@ -26,14 +26,6 @@ class FormatterPluginManager extends PluginManagerBase {
*/ */
protected $formatterOptions; protected $formatterOptions;
/**
* Overrides Drupal\Component\Plugin\PluginManagerBase:$defaults.
*/
protected $defaults = array(
'field_types' => array(),
'settings' => array(),
);
/** /**
* Constructs a FormatterPluginManager object. * Constructs a FormatterPluginManager object.
* *
...@@ -42,12 +34,19 @@ class FormatterPluginManager extends PluginManagerBase { ...@@ -42,12 +34,19 @@ class FormatterPluginManager extends PluginManagerBase {
* keyed by the corresponding namespace to look for plugin implementations, * keyed by the corresponding namespace to look for plugin implementations,
*/ */
public function __construct(\Traversable $namespaces) { public function __construct(\Traversable $namespaces) {
$this->discovery = new AnnotatedClassDiscovery('field/formatter', $namespaces); $annotation_namespaces = array('Drupal\field\Annotation' => $namespaces['Drupal\field']);
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition')); $this->discovery = new AnnotatedClassDiscovery('field/formatter', $namespaces, $annotation_namespaces, 'Drupal\field\Annotation\FieldFormatter');
$this->discovery = new AlterDecorator($this->discovery, 'field_formatter_info'); $this->discovery = new AlterDecorator($this->discovery, 'field_formatter_info');
$this->discovery = new CacheDecorator($this->discovery, 'field_formatter_types', 'field'); $this->discovery = new CacheDecorator($this->discovery, 'field_formatter_types', 'field');
}
$this->factory = new FormatterFactory($this->discovery); /**
* {@inheritdoc}
*/
public function createInstance($plugin_id, array $configuration) {
$plugin_definition = $this->discovery->getDefinition($plugin_id);
$plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
return new $plugin_class($plugin_id, $plugin_definition, $configuration['instance'], $configuration['settings'], $configuration['label'], $configuration['view_mode']);
} }
/** /**
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\field_test\Plugin\field\formatter; namespace Drupal\field_test\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
/** /**
* Plugin implementation of the 'field_test_default' formatter. * Plugin implementation of the 'field_test_default' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "field_test_default", * id = "field_test_default",
* module = "field_test", * module = "field_test",
* label = @Translation("Default"), * label = @Translation("Default"),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
*/ */
namespace Drupal\field_test\Plugin\field\formatter; namespace Drupal\field_test\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\field\Plugin\Type\Formatter\FormatterBase;
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
/** /**
* Plugin implementation of the 'field_empty_test' formatter. * Plugin implementation of the 'field_empty_test' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "field_empty_test", * id = "field_empty_test",
* module = "field_test", * module = "field_test",
* label = @Translation("Field empty test"), * label = @Translation("Field empty test"),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\field_test\Plugin\field\formatter; namespace Drupal\field_test\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
/** /**
* Plugin implementation of the 'field_test_multiple' formatter. * Plugin implementation of the 'field_test_multiple' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "field_test_multiple", * id = "field_test_multiple",
* module = "field_test", * module = "field_test",
* label = @Translation("Multiple"), * label = @Translation("Multiple"),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\field_test\Plugin\field\formatter; namespace Drupal\field_test\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
/** /**
* Plugin implementation of the 'field_test_with_prepare_view' formatter. * Plugin implementation of the 'field_test_with_prepare_view' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "field_test_with_prepare_view", * id = "field_test_with_prepare_view",
* module = "field_test", * module = "field_test",
* label = @Translation("With prepare step"), * label = @Translation("With prepare step"),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\file\Plugin\field\formatter; namespace Drupal\file\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
/** /**
* Plugin implementation of the 'file_default' formatter. * Plugin implementation of the 'file_default' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "file_default", * id = "file_default",
* module = "file", * module = "file",
* label = @Translation("Generic file"), * label = @Translation("Generic file"),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\file\Plugin\field\formatter; namespace Drupal\file\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
/** /**
* Plugin implementation of the 'file_rss_enclosure' formatter. * Plugin implementation of the 'file_rss_enclosure' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "file_rss_enclosure", * id = "file_rss_enclosure",
* module = "file", * module = "file",
* label = @Translation("RSS enclosure"), * label = @Translation("RSS enclosure"),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\file\Plugin\field\formatter; namespace Drupal\file\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin; use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase; use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
/** /**
* Plugin implementation of the 'file_table' formatter. * Plugin implementation of the 'file_table' formatter.
* *
* @Plugin( * @FieldFormatter(
* id = "file_table", * id = "file_table",
* module = "file", * module = "file",
* label = @Translation("Table of files"), * label = @Translation("Table of files"),
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\file\Plugin\field\formatter; namespace Drupal\file\Plugin\field\formatter;