Commit 56021b50 authored by catch's avatar catch

Issue #1955678 by swentel: Remove formatter and widgets legacy plugin.

parent d5a4cb43
<?php
/**
* @file
* Definition of Drupal\field\Plugin\Type\Formatter\FormatterLegacyDiscoveryDecorator.
*/
namespace Drupal\field\Plugin\Type\Formatter;
use Drupal\field\Plugin\Type\LegacyDiscoveryDecorator;
/**
* Custom decorator to add legacy widgets.
*
* Legacy formatters are discovered through the old
* hook_field_formatter_info() hook and handled by the
* Drupal\field\Plugin\field\formatter\LegacyFormatter class.
*
* @todo Remove once all core formatters have been converted.
*/
class FormatterLegacyDiscoveryDecorator extends LegacyDiscoveryDecorator {
/**
* Overrides Drupal\field\Plugin\Type\LegacyDiscoveryDecorator::$hook.
*/
protected $hook = 'field_formatter_info';
/**
* Overrides Drupal\field\Plugin\Type\LegacyDiscoveryDecorator::processDefinition().
*/
public function processDefinition(array &$definition) {
$definition['class'] = '\Drupal\field\Plugin\field\formatter\LegacyFormatter';
// Transform properties for which the format has changed.
if (isset($definition['field types'])) {
$definition['field_types'] = $definition['field types'];
unset($definition['field types']);
}
}
}
......@@ -12,7 +12,6 @@
use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\field\Plugin\Type\Formatter\FormatterLegacyDiscoveryDecorator;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
/**
......@@ -36,7 +35,6 @@ class FormatterPluginManager extends PluginManagerBase {
*/
public function __construct($namespaces) {
$this->discovery = new AnnotatedClassDiscovery('field', 'formatter', $namespaces);
$this->discovery = new FormatterLegacyDiscoveryDecorator($this->discovery);
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->discovery = new AlterDecorator($this->discovery, 'field_formatter_info');
$this->discovery = new CacheDecorator($this->discovery, 'field_formatter_types', 'field');
......
<?php
/**
* @file
* Definition of Drupal\field\Plugin\Type\LegacyDiscoveryDecorator.
*/
namespace Drupal\field\Plugin\Type;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Core\Plugin\Discovery\HookDiscovery;
/**
* Custom decorator to add legacy plugins.
*
* Legacy plugins are discovered through
* Drupal\Core\Plugin\Discovery\HookDiscovery, and handled by a legacy class.
*/
abstract class LegacyDiscoveryDecorator implements DiscoveryInterface {
/**
* The name of the hook for Drupal\Core\Plugin\Discovery\HookDiscovery.
*
* @var string
*/
protected $hook;
/**
* The decorated discovery object.
*
* @var Drupal\Component\Plugin\Discovery\DiscoveryInterface
*/
protected $decorated;
/**
* Creates a Drupal\field\Plugin\Discovery\LegacyDiscoveryDecorator object.
*
* @param Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
* The parent object implementing DiscoveryInterface that is being
* decorated.
*/
public function __construct(DiscoveryInterface $decorated) {
$this->decorated = $decorated;
}
/**
* Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition().
*/
public function getDefinition($plugin_id) {
$definitions = $this->getDefinitions();
return isset($definitions[$plugin_id]) ? $definitions[$plugin_id] : NULL;
}
/**
* Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions().
*/
public function getDefinitions() {
$definitions = $this->decorated->getDefinitions();
$legacy_discovery = new HookDiscovery($this->hook);
if ($legacy_definitions = $legacy_discovery->getDefinitions()) {
foreach ($legacy_definitions as $plugin_id => $definition) {
$this->processDefinition($definition);
if (isset($definition['behaviors']['default value'])) {
$definition['default_value'] = $definition['behaviors']['default value'];
unset($definition['behaviors']['default value']);
}
// Legacy widgets also need a plugin id.
$definition['id'] = $plugin_id;
$definitions[$plugin_id] = $definition;
}
}
return $definitions;
}
/**
* Massages a legacy plugin definition.
*
* @var array $definition
* A plugin definition, as discovered by
* Drupal\Core\Plugin\Discovery\HookDiscovery.
*
* @return array
* The massaged plugin definition.
*/
abstract public function processDefinition(array &$definition);
}
<?php
/**
* @file
* Definition of Drupal\field\Plugin\Type\Widget\WidgetLegacyDiscoveryDecorator.
*/
namespace Drupal\field\Plugin\Type\Widget;
use Drupal\field\Plugin\Type\LegacyDiscoveryDecorator;
/**
* Custom decorator to add legacy widgets.
*
* Legacy widgets are discovered through the old hook_field_widget_info() hook,
* and handled by the Drupal\field\Plugin\field\widget\LegacyWidget class.
*
* @todo Remove once all core widgets have been converted.
*/
class WidgetLegacyDiscoveryDecorator extends LegacyDiscoveryDecorator {
/**
* Overrides Drupal\field\Plugin\Type\LegacyDiscoveryDecorator::$hook.
*/
protected $hook = 'field_widget_info';
/**
* Overrides Drupal\field\Plugin\Type\LegacyDiscoveryDecorator::processDefinition().
*/
public function processDefinition(array &$definition) {
$definition['class'] = '\Drupal\field\Plugin\field\widget\LegacyWidget';
// Transform properties for which the format has changed.
if (isset($definition['field types'])) {
$definition['field_types'] = $definition['field types'];
unset($definition['field types']);
}
if (isset($definition['behaviors']['multiple values'])) {
$definition['multiple_values'] = $definition['behaviors']['multiple values'];
unset($definition['behaviors']['multiple values']);
}
}
}
......@@ -12,7 +12,6 @@
use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\field\Plugin\Type\Widget\WidgetLegacyDiscoveryDecorator;
/**
* Plugin type manager for field widgets.
......@@ -37,7 +36,6 @@ class WidgetPluginManager extends PluginManagerBase {
*/
public function __construct($namespaces) {
$this->discovery = new AnnotatedClassDiscovery('field', 'widget', $namespaces);
$this->discovery = new WidgetLegacyDiscoveryDecorator($this->discovery);
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->discovery = new AlterDecorator($this->discovery, 'field_widget_info');
$this->discovery = new CacheDecorator($this->discovery, 'field_widget_types', 'field');
......
<?php
/**
* @file
* Definition of Drupal\field\Plugin\field\formatter\LegacyFormatter.
*/
namespace Drupal\field\Plugin\field\formatter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
/**
* Plugin implementation for legacy formatters.
*
* This special implementation acts as a temporary BC layer for formatters that
* have not been converted to Plugins, and bridges new methods to the old-style
* hook_field_formatter_*() callbacks.
*
* This class is not discovered by the annotations reader, but referenced by
* the Drupal\field\Plugin\Discovery\LegacyDiscoveryDecorator.
*
* @todo Remove once all core formatters have been converted.
*/
class LegacyFormatter extends FormatterBase {
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
*/
public function settingsForm(array $form, array &$form_state) {
$definition = $this->getDefinition();
$function = $definition['module'] . '_field_formatter_settings_form';
// hook_field_formatter_settings_form() implementations read display
// properties directly from $instance. Put the actual properties we use
// here.
$instance = clone $this->instance;
$instance['display'][$this->viewMode] = array(
'type' => $this->getPluginId(),
'settings' => $this->getSettings(),
'label' => $this->label,
);
if (function_exists($function)) {
return $function($this->field, $instance, $this->viewMode, $form, $form_state);
}
return array();
}
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsSummary().
*/
public function settingsSummary() {
$definition = $this->getDefinition();
$function = $definition['module'] . '_field_formatter_settings_summary';
// hook_field_formatter_settings_summary() implementations read display
// properties directly from $instance. Put the actual properties we use
// here.
$instance = clone $this->instance;
$instance['display'][$this->viewMode] = array(
'type' => $this->getPluginId(),
'settings' => $this->getSettings(),
'label' => $this->label,
);
if (function_exists($function)) {
return $function($this->field, $instance, $this->viewMode);
}
}
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView().
*/
public function prepareView(array $entities, $langcode, array &$items) {
$definition = $this->getDefinition();
$function = $definition['module'] . '_field_formatter_prepare_view';
if (function_exists($function)) {
// Grab the entity type from the first entity.
$entity = current($entities);
$entity_type = $entity->entityType();
// hook_field_formatter_prepare_view() received an array of display properties,
// for each entity (the same hook could end up being called for different formatters,
// since one hook implementation could provide several formatters).
$display = array(
'type' => $this->getPluginId(),
'settings' => $this->getSettings(),
'label' => $this->label,
);
$displays = array();
foreach ($entities as $entity) {
$displays[$entity->id()] = $display;
}
$function($entity_type, $entities, $this->field, $this->instance, $langcode, $items, $displays);
}
}
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
*/
public function viewElements(EntityInterface $entity, $langcode, array $items) {
$definition = $this->getDefinition();
$function = $definition['module'] . '_field_formatter_view';
if (function_exists($function)) {
// hook_field_formatter_view() received an array of display properties,
$display = array(
'type' => $this->getPluginId(),
'settings' => $this->getSettings(),
'label' => $this->label,
);
return $function($entity, $this->field, $this->instance, $langcode, $items, $display);
}
}
}
<?php
/**
* @file
* Definition of Drupal\field\Plugin\field\widget\LegacyWidget.
*/
namespace Drupal\field\Plugin\field\widget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Widget\WidgetBase;
/**
* Plugin implementation for legacy widgets.
*
* This special implementation acts as a temporary BC layer for widgets that
* have not been converted to Plugins, and bridges new methods to the old-style
* hook_field_widget_*() callbacks.
*
* This class is not discovered by the annotations reader, but referenced by
* the Drupal\field\Plugin\Discovery\LegacyDiscoveryDecorator.
*
* @todo Remove once all core widgets have been converted.
*/
class LegacyWidget extends WidgetBase {
/**
* Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm().
*/
public function settingsForm(array $form, array &$form_state) {
$definition = $this->getDefinition();
$function = $definition['module'] . '_field_widget_settings_form';
if (function_exists($function)) {
return $function($this->field, $this->instance);
}
return array();
}
/**
* Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
*/
public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
$definition = $this->getDefinition();
$function = $definition['module'] . '_field_widget_form';
if (function_exists($function)) {
// hook_field_widget_form() implementations read widget properties directly
// from $instance. Put the actual properties we use here, which might have
// been altered by hook_field_widget_property().
$instance = clone $this->instance;
$instance['widget']['type'] = $this->getPluginId();
$instance['widget']['settings'] = $this->getSettings();
return $function($form, $form_state, $this->field, $instance, $langcode, $items, $delta, $element);
}
return array();
}
/**
* Overrides Drupal\field\Plugin\Type\Widget\WidgetBase::flagErrors().
*
* In D7, hook_field_widget_error() was supposed to call form_error() itself,
* whereas the new errorElement() method simply returns the element to flag.
* So we override the flagError() method to be more similar to the previous
* code in field_default_form_errors().
*/
public function flagErrors(EntityInterface $entity, $langcode, array $items, array $form, array &$form_state) {
$field_name = $this->field['field_name'];
$field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state);
if (!empty($field_state['errors'])) {
// Locate the correct element in the form.
$element = NestedArray::getValue($form_state['complete_form'], $field_state['array_parents']);
// Only set errors if the element is accessible.
if (!isset($element['#access']) || $element['#access']) {
$definition = $this->getDefinition();
$is_multiple = $definition['multiple_values'];
$function = $definition['module'] . '_field_widget_error';
$function_exists = function_exists($function);
foreach ($field_state['errors'] as $delta => $delta_errors) {
// For multiple single-value widgets, pass errors by delta.
// For a multiple-value widget, pass all errors to the main widget.
$error_element = $is_multiple ? $element : $element[$delta];
foreach ($delta_errors as $error) {
if ($function_exists) {
$function($error_element, $error, $form, $form_state);
}
else {
// Make sure that errors are reported (even incorrectly flagged) if
// the widget module fails to implement hook_field_widget_error().
form_error($error_element, $error['message']);
}
}
}
// Reinitialize the errors list for the next submit.
$field_state['errors'] = array();
field_form_set_state($form['#parents'], $field_name, $langcode, $form_state, $field_state);
}
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment