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 @@
namespace Drupal\datetime\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -17,7 +17,7 @@
/**
* Plugin implementation of the 'datetime_default' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "datetime_default",
* module = "datetime",
* label = @Translation("Default"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\datetime\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -16,7 +16,7 @@
/**
* Plugin implementation of the 'datetime_plain' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "datetime_plain",
* module = "datetime",
* label = @Translation("Plain"),
......
......@@ -133,6 +133,23 @@ function edit_library_info() {
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.
*/
......
......@@ -63,7 +63,7 @@ public function getEditor($formatter_type, FieldInstance $instance, array $items
// 'form' editor, since that can work for any field. Formatter definitions
// can use 'disabled' to explicitly opt out of in-place editing.
$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') {
return;
}
......
......@@ -7,7 +7,7 @@
namespace Drupal\email\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'email_mailto' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "email_mailto",
* module = "email",
* label = @Translation("Email"),
......
......@@ -7,7 +7,7 @@
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\Entity\EntityInterface;
use Drupal\entity_reference\RecursiveRenderingException;
......@@ -16,7 +16,7 @@
/**
* Plugin implementation of the 'entity reference rendered entity' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "entity_reference_entity_view",
* module = "entity_reference",
* label = @Translation("Rendered entity"),
......
......@@ -7,7 +7,7 @@
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\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
......
......@@ -7,7 +7,7 @@
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\Entity\EntityInterface;
use Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'entity reference ID' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "entity_reference_entity_id",
* module = "entity_reference",
* label = @Translation("Entity ID"),
......
......@@ -7,7 +7,7 @@
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\Entity\EntityInterface;
use Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'entity reference label' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "entity_reference_label",
* module = "entity_reference",
* 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 @@
namespace Drupal\field\Plugin\Type\Formatter;
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\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
......@@ -26,14 +26,6 @@ class FormatterPluginManager extends PluginManagerBase {
*/
protected $formatterOptions;
/**
* Overrides Drupal\Component\Plugin\PluginManagerBase:$defaults.
*/
protected $defaults = array(
'field_types' => array(),
'settings' => array(),
);
/**
* Constructs a FormatterPluginManager object.
*
......@@ -42,12 +34,19 @@ class FormatterPluginManager extends PluginManagerBase {
* keyed by the corresponding namespace to look for plugin implementations,
*/
public function __construct(\Traversable $namespaces) {
$this->discovery = new AnnotatedClassDiscovery('field/formatter', $namespaces);
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$annotation_namespaces = array('Drupal\field\Annotation' => $namespaces['Drupal\field']);
$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 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 @@
namespace Drupal\field_test\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'field_test_default' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "field_test_default",
* module = "field_test",
* label = @Translation("Default"),
......
......@@ -7,7 +7,7 @@
*/
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\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'field_empty_test' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "field_empty_test",
* module = "field_test",
* label = @Translation("Field empty test"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\field_test\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'field_test_multiple' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "field_test_multiple",
* module = "field_test",
* label = @Translation("Multiple"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\field_test\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'field_test_with_prepare_view' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "field_test_with_prepare_view",
* module = "field_test",
* label = @Translation("With prepare step"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\file\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'file_default' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "file_default",
* module = "file",
* label = @Translation("Generic file"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\file\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'file_rss_enclosure' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "file_rss_enclosure",
* module = "file",
* label = @Translation("RSS enclosure"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\file\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'file_table' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "file_table",
* module = "file",
* label = @Translation("Table of files"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\file\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'file_url_plain' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "file_url_plain",
* module = "file",
* label = @Translation("URL to file"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\image\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'image' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "image",
* module = "image",
* label = @Translation("Image"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\link\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'link' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "link",
* module = "link",
* label = @Translation("Link"),
......
......@@ -12,7 +12,7 @@
namespace Drupal\link\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
......@@ -20,7 +20,7 @@
/**
* Plugin implementation of the 'link_separate' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "link_separate",
* module = "link",
* label = @Translation("Separate link text and URL"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\number\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......
......@@ -7,7 +7,7 @@
namespace Drupal\number\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\number\Plugin\field\formatter\DefaultNumberFormatter;
......@@ -20,7 +20,7 @@
* for decimal and float fields on the other hand, in order to be able to use
* different settings.
*
* @Plugin(
* @FieldFormatter(
* id = "number_decimal",
* module = "number",
* label = @Translation("Default"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\number\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\number\Plugin\field\formatter\DefaultNumberFormatter;
......@@ -20,7 +20,7 @@
* for decimal and float fields on the other hand, in order to be able to use
* different settings.
*
* @Plugin(
* @FieldFormatter(
* id = "number_integer",
* module = "number",
* label = @Translation("Default"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\number\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'number_unformatted' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "number_unformatted",
* module = "number",
* label = @Translation("Unformatted"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\options\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'list_default' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "list_default",
* module = "options",
* label = @Translation("Default"),
......
......@@ -7,7 +7,7 @@
namespace Drupal\options\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
......@@ -15,7 +15,7 @@
/**
* Plugin implementation of the 'list_key' formatter.
*
* @Plugin(
* @FieldFormatter(
* id = "list_key",
* module = "options",