You are here

function hook_copy_form_values_to_transition_field_alter in Workflow 8

Implements hook_copy_form_values_to_transition_field_alter().

See also

#2899025 'Attached Field API type 'file' not working on WorkflowTransition'.

1 invocation of hook_copy_form_values_to_transition_field_alter()
WorkflowTransitionElement::copyFormValuesToTransition in src/Element/WorkflowTransitionElement.php
Implements ContentEntityForm::copyFormValuesToEntity().

File

./workflow.api.php, line 400
Hooks provided by the workflow module.

Code

function hook_copy_form_values_to_transition_field_alter(EntityInterface $entity, $context) {

  /** @var \Drupal\Core\Field\Entity\BaseFieldOverride $field */
  $field = $context['field'];
  $field_name = $context['field_name'];
  $user_input = $context['user_input'];

  // Workaround for issue 2899025, but works only with entity_browser module.
  // @see https://www.drupal.org/project/workflow/issues/2899025
  // Issue 'Attached field type 'file' not working on WorkflowTransition'
  if ($field
    ->getType() == 'file' && !empty($user_input['current'])) {
    workflow_debug(__FILE__, __FUNCTION__, __LINE__, '', '');

    // Avoid inserting two references to the same file.
    // Workaround for issue #2926094 'Avoid calling the WorkflowTransitionElement twice on a form'.
    $entity->{$field_name} = [];
    foreach ($user_input['current'] as $target_id => $values) {
      $data = [
        "target_id" => $target_id,
        "description" => $values["meta"]["description"],
      ];
      $entity->{$field_name}[] = $data;
    }
  }
}