You are here

public function Job::addItem in Translation Management Tool 8

Adds an item to the translation job.

Parameters

string $plugin: The plugin name.

string $item_type: The source item type.

string $item_id: The source item id.

Return value

\Drupal\tmgmt\JobItemInterface The job item that was added to the job or FALSE if it couldn't be saved.

Throws

\Drupal\tmgmt\TMGMTException On zero item word count.

Overrides JobInterface::addItem

File

src/Entity/Job.php, line 331

Class

Job
Entity class for the tmgmt_job entity.

Namespace

Drupal\tmgmt\Entity

Code

public function addItem($plugin, $item_type, $item_id) {
  $transaction = \Drupal::database()
    ->startTransaction();
  $is_new = FALSE;
  if ($this
    ->isNew()) {
    $this
      ->save();
    $is_new = TRUE;
  }
  $item = tmgmt_job_item_create($plugin, $item_type, $item_id, array(
    'tjid' => $this
      ->id(),
  ));
  $item
    ->save();
  if ($item
    ->getWordCount() == 0) {
    $transaction
      ->rollback();

    // In case we got word count 0 for the first job item, NULL tjid so that
    // if there is another addItem() call the rolled back job object will get
    // persisted.
    if ($is_new) {
      $this->tjid = NULL;
    }
    throw new TMGMTException('Job item @label (@type) has no translatable content.', array(
      '@label' => $item
        ->label(),
      '@type' => $item
        ->getSourceType(),
    ));
  }
  return $item;
}