public function WorkflowItem::storageSettingsForm in Workflow 8
Implements hook_field_settings_form() -> ConfigFieldItemInterface::settingsForm().
Overrides ListItemBase::storageSettingsForm
File
- src/
Plugin/ Field/ FieldType/ WorkflowItem.php, line 162
Class
- WorkflowItem
- Plugin implementation of the 'workflow' field type.
Namespace
Drupal\workflow\Plugin\Field\FieldTypeCode
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = [];
// Create list of all Workflow types. Include an initial empty value.
// Validate each workflow, and generate a message if not complete.
$workflows = workflow_get_workflow_names(FALSE);
// @todo D8: add this to WorkflowFieldConstraintValidator.
// Set message, if no 'validated' workflows exist.
if (count($workflows) == 1) {
$this
->messenger()
->addWarning($this
->t('You must <a href=":create">create at least one workflow</a>
before content can be assigned to a workflow.', [
':create' => Url::fromRoute('entity.workflow_type.collection')
->toString(),
]));
}
// Validate via annotation WorkflowFieldConstraint. Show a message for each error.
$violation_list = $this
->validate();
/** @var \Symfony\Component\Validator\ConstraintViolation $violation */
foreach ($violation_list
->getIterator() as $violation) {
switch ($violation
->getPropertyPath()) {
case 'fieldnameOnComment':
// @todo D8: CommentForm & constraints on storageSettingsForm().
// A 'comment' field name MUST be equal to content field name.
// @todo Fix fields on a non-relevant entity_type.
$this
->messenger()
->addError($violation
->getMessage());
$workflows = [];
break;
default:
break;
}
}
// @todo D8: CommentForm & constraints on storageSettingsForm.
// Set the required workflow_type on 'comment' fields.
// N.B. the following must BELOW the (count($workflows) == 1) snippet.
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage */
$field_storage = $this
->getFieldDefinition()
->getFieldStorageDefinition();
if (!$this
->getSetting('workflow_type') && $field_storage
->getTargetEntityTypeId() == 'comment') {
$field_name = $field_storage
->get('field_name');
$workflows = [];
foreach (_workflow_info_fields($entity = NULL, $entity_type = '', $entity_bundle = '', $field_name) as $key => $info) {
if ($info
->getName() == $field_name && $info
->getTargetEntityTypeId() !== 'comment') {
$wid = $info
->getSetting('workflow_type');
$workflow = Workflow::load($wid);
$workflows[$wid] = $workflow
->label();
}
}
}
// Let the user choose between the available workflow types.
$wid = $this
->getSetting('workflow_type');
$url = Url::fromRoute('entity.workflow_type.collection')
->toString();
$element['workflow_type'] = [
'#type' => 'select',
'#title' => $this
->t('Workflow type'),
'#options' => $workflows,
'#default_value' => $wid,
'#required' => TRUE,
'#disabled' => $has_data,
'#description' => $this
->t('Choose the Workflow type. Maintain workflows
<a href=":url">here</a>.', [
':url' => $url,
]),
];
// Get a string representation to show all options.
/*
* Overwrite ListItemBase::storageSettingsForm().
*/
if ($wid) {
$allowed_values = WorkflowState::loadMultiple([], $wid);
// $allowed_values_function = $this->getSetting('allowed_values_function');
$element['allowed_values'] = [
'#type' => 'textarea',
'#title' => $this
->t('Allowed values for the selected Workflow type'),
'#default_value' => $wid ? $this
->allowedValuesString($allowed_values) : [],
'#rows' => count($allowed_values),
'#access' => $wid ? TRUE : FALSE,
// User can see the data,
'#disabled' => TRUE,
// .. but cannot change them.
'#element_validate' => [
[
get_class($this),
'validateAllowedValues',
],
],
'#field_has_data' => $has_data,
'#field_name' => $this
->getFieldDefinition()
->getName(),
'#entity_type' => $this
->getEntity() ? $this
->getEntity()
->getEntityTypeId() : '',
'#allowed_values' => $allowed_values,
'#description' => $this
->allowedValuesDescription(),
];
}
return $element;
}