Commit 7680ceb6 authored by mglaman's avatar mglaman Committed by bojanz

Issue #2757317 by mglaman, moshe weitzman, bojanz: Provide field formatter to...

Issue #2757317 by mglaman, moshe weitzman, bojanz: Provide field formatter to provide transitions as actionable buttons
parent 8bf77e33
<?php
namespace Drupal\state_machine\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class StateTransitionForm extends FormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity.
*
* @var \Drupal\Core\Entity\FieldableEntityInterface
*/
protected $entity;
/**
* Constructs a new StateTransitionForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'state_machine_state_transition_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$field_name = $form_state->get('field_name');
$entity_type = $form_state->get('entity_type');
$entity_id = $form_state->get('entity_id');
$this->entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id);
/** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state_item */
$state_item = $this->entity->get($field_name)->first();
$form['actions'] = [
'#type' => 'container',
];
foreach ($state_item->getTransitions() as $transition_id => $transition) {
$form['actions'][$transition_id] = [
'#type' => 'submit',
'#value' => $transition->getLabel(),
'#submit' => ['::submitForm'],
'#transition' => $transition,
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state->getTriggeringElement();
/** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state_item */
$state_item = $this->entity->get($form_state->get('field_name'))->first();
$state_item->applyTransition($triggering_element['#transition']);
$this->entity->save();
}
}
<?php
namespace Drupal\state_machine\Plugin\Field\FieldFormatter;
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\Plugin\ContainerFactoryPluginInterface;
use Drupal\state_machine\Form\StateTransitionForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'state_transition_form' formatter.
*
* @FieldFormatter(
* id = "state_transition_form",
* label = @Translation("Transition form"),
* field_types = {
* "state",
* },
* )
*/
class StateTransitionFormFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* Constructs a new StateTransitionFormFormatter object.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The form builder.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, FormBuilderInterface $form_builder) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->formBuilder = $form_builder;
}
/**
* {@inheritdoc}
*/
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('form_builder')
);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
// Do not show the form if the user isn't allowed to modify the entity.
if (!$items->getEntity()->access('update')) {
return [];
}
$form_state = (new FormState())->setFormState([
'entity_type' => $items->getEntity()->getEntityTypeId(),
'entity_id' => $items->getEntity()->id(),
'field_name' => $items->getFieldDefinition()->getName()
]);
// $elements needs a value for each delta. State fields can't be multivalue,
// so it's safe to hardcode 0.
$elements = [];
$elements[0] = $this->formBuilder->buildForm(StateTransitionForm::class, $form_state);
return $elements;
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition->getType() == '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