You are here

public static function Job::preCreate in Translation Management Tool 8

Changes the values of an entity before it is created.

Load defaults for example.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

mixed[] $values: An array of values to set, keyed by property name. If the entity type has bundles the bundle key has to be specified.

Overrides EntityBase::preCreate

File

src/Entity/Job.php, line 153

Class

Job
Entity class for the tmgmt_job entity.

Namespace

Drupal\tmgmt\Entity

Code

public static function preCreate(EntityStorageInterface $storage, array &$values) {
  parent::preCreate($storage, $values);

  // If either the source or target language is empty.
  if (empty($values['source_language']) || empty($values['target_language'])) {
    $languages = tmgmt_available_languages();
    if (empty($values['source_language'])) {
      $values['source_language'] = key($languages);
    }
    if (empty($values['target_language'])) {

      // Values might be an array, simplify it to a scalar langcode.
      $source_language = $values['source_language'];
      while (is_array($source_language)) {
        $source_language = reset($source_language);
      }

      // Make sure the source language is not available as target language,
      // use the next best value as the default.
      unset($languages[$source_language]);
      $values['target_language'] = key($languages);
    }
  }
}