View source
<?php
namespace Drupal\state_machine\Plugin\Field\FieldFormatter;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\state_machine\Form\StateTransitionForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
class StateTransitionFormFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $classResolver;
protected $formBuilder;
protected $entityTypeManager;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ClassResolverInterface $class_resolver, FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->classResolver = $class_resolver;
$this->formBuilder = $form_builder;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('class_resolver'), $container
->get('form_builder'), $container
->get('entity_type.manager'));
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$entity = $items
->getEntity();
if (!$entity
->access('update')) {
return [];
}
$form_object = $this->classResolver
->getInstanceFromDefinition(StateTransitionForm::class);
$form_object
->setEntity($entity);
$form_object
->setFieldName($items
->getFieldDefinition()
->getName());
$form_state_additions = [];
if ($this
->supportsConfirmationForm()) {
$form_state_additions += [
'require_confirmation' => (bool) $this
->getSetting('require_confirmation'),
'use_modal' => (bool) $this
->getSetting('use_modal'),
];
}
$form_state = (new FormState())
->setFormState($form_state_additions);
$elements = [];
$elements[0] = $this->formBuilder
->buildForm($form_object, $form_state);
return $elements;
}
public static function defaultSettings() {
return [
'require_confirmation' => FALSE,
'use_modal' => FALSE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$supports_confirmation_form = $this
->supportsConfirmationForm();
$form['require_confirmation'] = [
'#title' => $this
->t('Require confirmation before applying the state transition'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('require_confirmation'),
'#access' => $supports_confirmation_form,
];
$form['use_modal'] = [
'#title' => $this
->t('Display confirmation in a modal dialog'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('use_modal'),
'#states' => [
'visible' => [
':input[name*="require_confirmation"]' => [
'checked' => TRUE,
],
],
],
'#access' => $supports_confirmation_form,
];
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
if (!$this
->supportsConfirmationForm()) {
return $summary;
}
if ($this
->getSetting('require_confirmation')) {
$summary[] = $this
->t('Require confirmation before applying the state transition.');
if ($this
->getSetting('use_modal')) {
$summary[] = $this
->t('Display confirmation in a modal dialog.');
}
}
else {
$summary[] = $this
->t('Do not require confirmation before applying the state transition.');
}
return $summary;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition
->getType() == 'state';
}
protected function supportsConfirmationForm() {
$entity_type = $this->entityTypeManager
->getDefinition($this->fieldDefinition
->getTargetEntityTypeId());
return $entity_type
->hasLinkTemplate('state-transition-form');
}
}