View source
<?php
namespace Drupal\flexiform\Plugin\FormComponentType;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetPluginManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\flexiform\FlexiformEntityFormDisplay;
use Drupal\flexiform\FormComponent\FormComponentBase;
use Drupal\flexiform\FormComponent\ContainerFactoryFormComponentInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldWidgetComponent extends FormComponentBase implements ContainerFactoryFormComponentInterface {
protected $renderer;
protected $fieldDefinition;
protected $pluginManager;
protected $moduleHandler;
public static function create(ContainerInterface $container, $name, array $options, FlexiformEntityFormDisplay $form_display) {
return new static($name, $options, $form_display, $container
->get('plugin.manager.field.widget'), $container
->get('module_handler'));
}
public function __construct($name, $options, FlexiformEntityFormDisplay $form_display, WidgetPluginManager $plugin_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($name, $options, $form_display);
$this->moduleHandler = $module_handler;
$this->pluginManager = $plugin_manager;
}
public function setFieldDefinition(FieldDefinitionInterface $field_definition) {
$this->fieldDefinition = $field_definition;
}
protected function getRenderer() {
if (!empty($this->renderer)) {
return $this->renderer;
}
if (isset($this->options['type']) && ($definition = $this
->getFieldDefinition())) {
$widget = $this->pluginManager
->getInstance([
'field_definition' => $definition,
'form_mode' => $this
->getFormDisplay()
->getOriginalMode(),
'prepare' => FALSE,
'configuration' => $this->options,
]);
}
else {
$widget = NULL;
}
$this->renderer = $widget;
return $widget;
}
public function getFieldDefinition() {
return $this->fieldDefinition;
}
public function render(array &$form, FormStateInterface $form_state, RendererInterface $renderer) {
if ($widget = $this
->getRenderer()) {
if (strpos($this->name, ':')) {
list($namespace, $field_name) = explode(':', $this->name, 2);
$form['#parents'][] = $namespace;
if ($form_entity = $this
->getFormEntityManager()
->getEntity($namespace)) {
$items = $form_entity
->get($field_name);
}
else {
return;
}
}
else {
$items = $this
->getFormEntityManager()
->getEntity('')
->get($this->name);
}
$items
->filterEmptyItems();
$form[$this->name] = $widget
->form($items, $form, $form_state);
$form[$this->name]['#access'] = $items
->access('edit');
$form[$this->name]['#weight'] = $this->options['weight'];
$field_definition = $this
->getFieldDefinition();
$renderer
->addCacheableDependency($form[$this->name], $field_definition);
$renderer
->addCacheableDependency($form[$this->name], $field_definition
->getFieldStorageDefinition());
}
}
public function extractFormValues(array $form, FormStateInterface $form_state) {
$original_parents = $form['#parents'];
array_pop($original_parents);
$form = [
'#parents' => $original_parents,
$this->name => $form,
];
if (strpos($this->name, ':')) {
list($namespace, $field_name) = explode(':', $this->name, 2);
}
else {
$namespace = '';
$field_name = $this->name;
}
if ($context = $this
->getFormEntityManager()
->getContext($namespace)) {
$entity_object = $context
->getContextValue();
$items = $entity_object->{$field_name};
if ($widget = $this
->getRenderer()) {
$widget
->extractFormValues($items, $form, $form_state);
}
}
}
public function getAdminLabel() {
if (count($this
->getFormEntityManager()
->getFormEntities()) > 1) {
$namespace = $this
->getEntityNamespace();
return $this
->getFieldDefinition()
->getLabel() . ' [' . $this
->getFormEntityManager()
->getContext($namespace)
->getContextDefinition()
->getLabel() . ']';
}
return $this
->getFieldDefinition()
->getLabel();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$plugin = $this
->getRenderer();
if ($plugin) {
$settings_form = $plugin
->settingsForm($form, $form_state);
$third_party_settings_form = $this
->thirdPartySettingsForm($form, $form_state);
if ($settings_form || $third_party_settings_form) {
return [
'settings' => $settings_form,
'third_party_settings' => $third_party_settings_form,
];
}
}
return [];
}
public function settingsSummary() {
$summary = [];
if ($plugin = $this
->getRenderer()) {
$summary = $plugin
->settingsSummary();
}
$context = [
'widget' => $plugin,
'field_definition' => $this
->getFieldDefinition(),
'form_mode' => $this
->getFormDisplay()
->getMode(),
];
$this->moduleHandler
->alter('field_widget_settings_summary', $summary, $context);
if ($plugin && ($field_def = $plugin
->getThirdPartySetting('flexiform', 'field_definition'))) {
if (!empty($field_def['label'])) {
$summary[] = t('Label override: @label', [
'@label' => $field_def['label'],
]);
}
}
return $summary;
}
public function settingsFormSubmit($values, array $form, FormStateInterface $form_state) {
$default_settings = $this->pluginManager
->getDefaultSettings($this->options['type']);
$options['settings'] = isset($values['settings']) ? array_intersect_key($values['settings'], $default_settings) : [];
$options['third_party_settings'] = isset($values['third_party_settings']) ? $values['third_party_settings'] : [];
return $options;
}
protected function thirdPartySettingsForm(array $form, FormStateInterface $form_state) {
$plugin = $this
->getRenderer();
$settings_form = [];
foreach ($this->moduleHandler
->getImplementations('field_widget_third_party_settings_form') as $module) {
$settings_form[$module] = $this->moduleHandler
->invoke($module, 'field_widget_third_party_settings_form', [
$this
->getRenderer(),
$this
->getFieldDefinition(),
$this
->getFormDisplay()
->getMode(),
$form,
$form_state,
]);
}
$field_def = $plugin
->getThirdPartySetting('flexiform', 'field_definition');
$settings_form['flexiform']['field_definition']['label'] = [
'#type' => 'textfield',
'#title' => t('Label Override'),
'#default_value' => !empty($field_def['label']) ? $field_def['label'] : '',
];
return $settings_form;
}
}