You are here

public function TMGMTJob::defaultLabel in Translation Management Tool 7

Defines the entity label if the 'entity_class_label' callback is used.

Specify 'entity_class_label' as 'label callback' in hook_entity_info() to let the entity label point to this method. Override this in order to implement a custom default label.

Overrides Entity::defaultLabel

File

entity/tmgmt.entity.job.inc, line 121

Class

TMGMTJob
Entity class for the tmgmt_job entity.

Code

public function defaultLabel() {

  // In some cases we might have a user-defined label.
  if (!empty($this->label)) {
    return $this->label;
  }
  $items = $this
    ->getItems();
  $count = count($items);
  if ($count > 0) {
    $source_label = reset($items)
      ->getSourceLabel();
    $t_args = array(
      '!title' => $source_label,
      '!more' => $count - 1,
    );
    $label = format_plural($count, '!title', '!title and !more more', $t_args);

    // If the label length exceeds maximum allowed then cut off exceeding
    // characters from the title and use it to recreate the label.
    if (strlen($label) > TMGMT_JOB_LABEL_MAX_LENGTH) {
      $max_length = strlen($source_label) - (strlen($label) - TMGMT_JOB_LABEL_MAX_LENGTH);
      $source_label = truncate_utf8($source_label, $max_length, TRUE);
      $t_args['!title'] = $source_label;
      $label = format_plural($count, '!title', '!title and !more more', $t_args);
    }
  }
  else {
    $wrapper = entity_metadata_wrapper($this->entityType, $this);
    $source = $wrapper->source_language
      ->label();
    if (empty($source)) {
      $source = '?';
    }
    $target = $wrapper->target_language
      ->label();
    if (empty($target)) {
      $target = '?';
    }
    $label = t('From !source to !target', array(
      '!source' => $source,
      '!target' => $target,
    ));
  }
  return $label;
}