WorkflowsFieldItem.php in Workflows Field 8
File
src/Plugin/Field/FieldType/WorkflowsFieldItem.php
View source
<?php
namespace Drupal\workflows_field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\OptionsProviderInterface;
use Drupal\workflows\Entity\Workflow;
use Drupal\workflows\StateInterface;
class WorkflowsFieldItem extends FieldItemBase implements OptionsProviderInterface {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('State'))
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 64,
],
],
];
}
public static function defaultStorageSettings() {
$settings = [
'workflow' => NULL,
];
return $settings + parent::defaultStorageSettings();
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$workflows = Workflow::loadMultipleByType('workflows_field');
$options = [];
foreach ($workflows as $workflow) {
$options[$workflow
->id()] = $workflow
->label();
}
$element = [];
$element['workflow'] = [
'#title' => $this
->t('Workflow'),
'#required' => TRUE,
'#default_value' => $this
->getSetting('workflow'),
'#type' => 'select',
'#options' => $options,
];
return $element;
}
public function getPossibleValues(AccountInterface $account = NULL) {
return array_keys($this
->getPossibleOptions($account));
}
public function getPossibleOptions(AccountInterface $account = NULL) {
$workflow = $this
->getWorkflow();
if (!$workflow) {
return [];
}
$state_labels = array_map(function ($state) {
return $state
->label();
}, $workflow
->getTypePlugin()
->getStates());
return $state_labels;
}
public function getSettableValues(AccountInterface $account = NULL) {
return array_keys($this
->getSettableOptions($account));
}
public function getSettableOptions(AccountInterface $account = NULL) {
$field_name = $this
->getFieldDefinition()
->getName();
$value = $this
->getEntity()
->get($field_name)->value;
$workflow = $this
->getWorkflow();
$type = $workflow
->getTypePlugin();
$allowed_states = $type
->getStates();
if ($value && $type
->hasState($value) && ($current = $type
->getState($value))) {
$allowed_states = array_filter($allowed_states, function (StateInterface $state) use ($current, $workflow, $account) {
if ($current
->id() === $state
->id()) {
return TRUE;
}
$valid_transition = $current
->canTransitionTo($state
->id());
if (!$valid_transition || !$account) {
return $valid_transition;
}
$transition = $current
->getTransitionTo($state
->id());
return $account
->hasPermission(sprintf('use %s transition %s', $workflow
->id(), $transition
->id()));
});
}
$state_labels = array_map(function ($state) {
return $state
->label();
}, $allowed_states);
return $state_labels;
}
public function applyDefaultValue($notify = TRUE) {
if ($workflow = $this
->getWorkflow()) {
$initial_state = $workflow
->getTypePlugin()
->getInitialState();
$this
->setValue([
'value' => $initial_state
->id(),
], $notify);
}
return $this;
}
public static function calculateStorageDependencies(FieldStorageDefinitionInterface $field_definition) {
$dependencies['config'][] = sprintf('workflows.workflow.%s', $field_definition
->getSetting('workflow'));
return $dependencies;
}
public function getWorkflow() {
return !empty($this
->getSetting('workflow')) ? Workflow::load($this
->getSetting('workflow')) : NULL;
}
}