예제 #1
0
 /**
  * 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.
  *
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  * @param array $data
  *   Flattened array of translation data items.
  * @param string $parent_key
  *   The key for $data.
  *
  * @return 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']) && \Drupal::service('tmgmt.data')->filterData($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;
             $item_element['status'] = $this->buildStatusRenderArray($this->entity->isAccepted() ? TMGMT_DATA_ITEM_STATE_ACCEPTED : $data_item['#status']);
             $item_element['actions'] = array('#type' => 'container');
             $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);
             // Build source and translation areas.
             $item_element = $this->buildSource($item_element, $data_item, $rows, $form_state);
             $item_element = $this->buildTranslation($item_element, $data_item, $rows, $form_state);
             $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;
 }