Build form elements for the review form using flattened data items.
@todo Mention in the api documentation that the char '|' is not allowed in field names.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $data: Flattened array of translation data items.
string $parent_key: The key for $data.
array|NULL Render array with the form element, or NULL if the text is not set.
function reviewFormElement(FormStateInterface $form_state, $data, $parent_key) {
$review_element = NULL;
foreach (Element::children($data) as $key) {
$data_item = $data[$key];
if ((isset($data_item['#text']) || isset($data_item['#file'])) && \Drupal::service('tmgmt.data')
->filterDataWithFiles($data_item)) {
// The char sequence '][' confuses the form API so we need to replace
// it when using it for the form field name.
$field_name = str_replace('][', '|', $key);
// Ensure that the review form structure is initialized.
$review_element['#theme'] = 'tmgmt_data_items_form';
$review_element['#ajaxid'] = $ajax_id = tmgmt_review_form_element_ajaxid($parent_key);
$review_element['#top_label'] = array_shift($data_item['#parent_label']);
$leave_label = array_pop($data_item['#parent_label']);
// Data items are grouped based on their key hierarchy, calculate the
// group key and ensure that the group is initialized.
$group_name = substr($field_name, 0, strrpos($field_name, '|'));
if (empty($group_name)) {
$group_name = '_none';
}
if (!isset($review_element[$group_name])) {
$review_element[$group_name] = [
'#group_label' => $data_item['#parent_label'],
];
}
// Initialize the form element for the given data item and make it
// available as $element.
$review_element[$group_name][$field_name] = array(
'#tree' => TRUE,
);
$item_element =& $review_element[$group_name][$field_name];
$item_element['label']['#markup'] = $leave_label;
$data_item_status = $data_item['#status'];
$item_element['status'] = $this
->buildStatusRenderArray($this->entity
->isAccepted() ? TMGMT_DATA_ITEM_STATE_ACCEPTED : $data_item_status);
$is_preliminary = $data_item_status == TMGMT_DATA_ITEM_STATE_PRELIMINARY;
if ($is_preliminary) {
$form_state
->set('has_preliminary_items', $is_preliminary);
}
else {
$form_state
->set('all_preliminary', FALSE);
}
$item_element['actions'] = array(
'#type' => 'container',
'#access' => !$is_preliminary,
);
$item_element['below_actions'] = [
'#type' => 'container',
];
// Check if the field has a text format attached and check access.
if (!empty($data_item['#format'])) {
$format_id = $data_item['#format'];
/** @var \Drupal\filter\Entity\FilterFormat $format */
$format = FilterFormat::load($format_id);
if (!$format || !$format
->access('use')) {
$item_element['actions']['#access'] = FALSE;
$form_state
->set('accept_item', FALSE);
}
}
$item_element['actions'] += $this
->buildActions($data_item, $key, $field_name, $ajax_id);
// Manage the height of the textareas, depending on the length of the
// description. The minimum number of rows is 3 and the maximum is 15.
$rows = ceil(strlen($data_item['#text'] ?? '') / 100);
$rows = min($rows, 15);
$rows = max($rows, 3);
// Allow other modules to change the source and translation texts,
// for example to mask HTML-tags.
$source_text = $data_item['#text'] ?? '';
$translation_text = $data_item['#translation']['#text'] ?? '';
$contexts = [
'data_item' => $data_item,
'job_item' => $this->entity,
];
\Drupal::moduleHandler()
->alter('tmgmt_data_item_text_output', $source_text, $translation_text, $contexts);
// Build source and translation areas.
$item_element = $this
->buildSource($item_element, $source_text, $data_item, $rows, $form_state);
$item_element = $this
->buildTranslation($item_element, $translation_text, $data_item, $rows, $form_state, $is_preliminary);
$item_element = $this
->buildChangedSource($item_element, $form_state, $field_name, $key, $ajax_id);
if (isset($form_state
->get('validation_messages')[$field_name])) {
$item_element['below']['validation'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'tmgmt_validation_message',
'messages',
'messages--warning',
],
],
'message' => [
'#markup' => Html::escape($form_state
->get('validation_messages')[$field_name]),
],
];
}
// Give the translator UI controller a chance to affect the data item element.
if ($this->entity
->hasTranslator()) {
$item_element = \Drupal::service('plugin.manager.tmgmt.translator')
->createUIInstance($this->entity
->getTranslator()
->getPluginId())
->reviewDataItemElement($item_element, $form_state, $key, $parent_key, $data_item, $this->entity);
// Give the source ui controller a chance to affect the data item element.
$item_element = \Drupal::service('plugin.manager.tmgmt.source')
->createUIInstance($this->entity
->getPlugin())
->reviewDataItemElement($item_element, $form_state, $key, $parent_key, $data_item, $this->entity);
}
}
}
return $review_element;
}