Error message

Warning: count(): Parameter must be an array or an object that implements Countable in _api_make_match_member_link() (line 1230 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).

public function JobItemForm::save

Overrides Drupal\Core\Entity\EntityForm::save().

File

src/Form/JobItemForm.php, line 327

Class

JobItemForm
Form controller for the job item edit forms.

Namespace

Drupal\tmgmt\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $item = $this->entity;

  // First invoke the submit method on the source controller.
  $source_ui = $this->sourceManager
    ->createUIInstance($item
    ->getPlugin());
  $source_ui
    ->reviewFormSubmit($form, $form_state, $item);

  // Invoke the submit method on the translator controller (if available).
  if ($item
    ->getTranslator()) {
    $translator_ui = $this->translatorManager
      ->createUIInstance($item
      ->getTranslator()
      ->getPluginId());
    $translator_ui
      ->reviewFormSubmit($form, $form_state, $item);
  }

  // Write changes back to item.
  $data_service = \Drupal::service('tmgmt.data');
  foreach ($form_state
    ->getValues() as $key => $value) {
    if (is_array($value) && isset($value['translation'])) {

      // Update the translation, this will only update the translation in case
      // it has changed. We have two different cases, the first is for nested
      // texts.
      $data = NULL;
      if (!is_array($value['translation']) || isset($value['translation']['value'])) {
        $text = is_array($value['translation']) ? $value['translation']['value'] : $value['translation'];

        // Unmask the translation's HTML tags.
        $data_item = $item
          ->getData($data_service
          ->ensureArrayKey($key));
        $contexts = [
          'data_item' => $data_item,
          'job_item' => $this->entity,
        ];
        \Drupal::moduleHandler()
          ->alter('tmgmt_data_item_text_input', $text, $contexts);
        $data = [
          '#text' => $text,
          '#origin' => 'local',
        ];
        if ($data['#text'] == '' && $item
          ->isActive() && $form_state
          ->getTriggeringElement()['#value'] != '✓') {
          $data = NULL;
          continue;
        }
      }
      elseif (is_array($value['translation'])) {
        $data = [
          '#file' => $value['translation'][0] ?? NULL,
          '#origin' => 'local',
        ];
      }
      $current_data_status = $data_item['#status'];
      $item
        ->addTranslatedData($data, $key, $current_data_status);
    }
  }

  // Check if the user clicked on 'Accept', 'Submit' or 'Reject'.
  if (!empty($form['actions']['accept']) && $form_state
    ->getTriggeringElement()['#value'] == $form['actions']['accept']['#value']) {
    $item
      ->acceptTranslation();

    // Print all messages that have been saved while accepting the reviewed
    // translation.
    foreach ($item
      ->getMessagesSince() as $message) {

      // Ignore debug messages.
      if ($message
        ->getType() == 'debug') {
        continue;
      }
      if ($text = $message
        ->getMessage()) {
        $this
          ->messenger()
          ->addMessage(new FormattableMarkup($text, []), $message
          ->getType());
      }
    }
  }
  if ($form_state
    ->getTriggeringElement()['#value'] == $form['actions']['save']['#value'] && isset($data)) {
    if ($item
      ->getSourceUrl()) {
      $message = t('The translation for <a href=:job>@job_title</a> has been saved successfully.', [
        ':job' => $item
          ->getSourceUrl()
          ->toString(),
        '@job_title' => $item
          ->label(),
      ]);
    }
    else {
      $message = t('The translation has been saved successfully.');
    }
    $this
      ->messenger()
      ->addStatus($message);
  }
  $item
    ->save();
  $item
    ->getJob()
    ->isContinuous() ? $form_state
    ->setRedirect('entity.tmgmt_job_item.canonical', [
    'tmgmt_job_item' => $item
      ->id(),
  ]) : $form_state
    ->setRedirectUrl($item
    ->getJob()
    ->toUrl());
}