You are here

function auto_entitylabel_set_title in Automatic Entity Label 7

Sets the automatically generated entitylabel for the entity.

3 calls to auto_entitylabel_set_title()
auto_entitylabel_entity_presave in ./auto_entitylabel.module
Implements hook_entity_presave().
auto_entitylabel_entity_update_action in ./auto_entitylabel.module
Update action wrapper.
auto_entitylabel_exit in ./auto_entitylabel.module
Implements hook_exit().

File

./auto_entitylabel.module, line 269
Allows hiding of entity label fields and automatic label creation.

Code

function auto_entitylabel_set_title(&$entity, $type) {
  $settings = _auto_entitylabel_get_settings($entity, $type);

  // Generate title in different languages?
  $multilingual = FALSE;

  // Support for title module.
  $entity_info = entity_get_info($type);
  list(, , $bundle) = entity_extract_ids($type, $entity);
  $title_field_name = FALSE;
  $title_languages = array();
  if (module_exists('title') && title_field_replacement_enabled($type, $bundle, $settings['title'])) {
    $title_field_name = $entity_info['field replacement'][$settings['title']]['instance']['field_name'];
    $field_info = field_info_field($title_field_name);
    $title_languages = field_available_languages($type, $field_info);
    $multilingual = count($title_languages) > 1;
  }

  // Remove LANGUAGE_NONE from array of languages.
  if (($key = array_search(LANGUAGE_NONE, $title_languages, TRUE)) && auto_entitylabel_entity_language($type, $entity) !== LANGUAGE_NONE) {
    unset($title_languages[$key]);
  }

  // Generate titles.
  $titles = array();
  $pattern = variable_get('auto_entitylabel_pattern_' . $settings['key'], '');
  if (trim($pattern)) {
    $entity->changed = REQUEST_TIME;
    if ($multilingual) {
      foreach ($title_languages as $language) {
        $titles[$language] = _auto_entitylabel_patternprocessor($pattern, $entity, $type, $language);
      }
    }
    else {
      $titles[LANGUAGE_NONE] = _auto_entitylabel_patternprocessor($pattern, $entity, $type);
    }
  }
  elseif ($type == 'node' && !empty($entity->nid)) {
    $titles[LANGUAGE_NONE] = t('@bundle @node-id', array(
      '@bundle' => $bundle,
      '@node-id' => $entity->nid,
    ));
  }
  else {
    $titles[LANGUAGE_NONE] = t('@bundle', array(
      '@bundle' => $bundle,
    ));
  }
  $clone = clone $entity;
  drupal_alter('auto_entitylabel_title', $titles, $clone);

  // Ensure the generated title isn't too long.
  $max_length = auto_entitylabel_max_length($type, $settings['title']);
  foreach ($titles as $k => $v) {
    $titles[$k] = drupal_substr($v, 0, $max_length);
  }

  // Save titles on entity (field)
  if (module_exists('title') && title_field_replacement_enabled($type, $bundle, $settings['title'])) {
    foreach ($titles as $lang => $title) {
      if (!isset($entity->{$title_field_name}[$lang][0]['value']) || $entity->{$title_field_name}[$lang][0]['value'] != $title) {
        $entity->{$title_field_name}[$lang][0]['format'] = NULL;
        $entity->{$title_field_name}[$lang][0]['safe_value'] = check_plain($title);
        $entity->{$title_field_name}[$lang][0]['value'] = $title;
        $entity->auto_entitylabel_changed = TRUE;
      }
    }
  }

  // Save title on entity (non-field).  This needs be done even if field_title
  // above is updated, because the title module automatically syncs changes
  // from the "non field title" to the "title field". Without this line we end
  // up getting AUTO_ENTITYLABEL_PLACEHOLDER as the title.
  $entity_language = auto_entitylabel_entity_language($type, $entity);
  $title = isset($titles[$entity_language]) ? $titles[$entity_language] : $titles[LANGUAGE_NONE];
  if (!isset($entity->{$settings['title']}) || $entity->{$settings['title']} != $title) {
    $entity->{$settings['title']} = $title;
    $entity->auto_entitylabel_changed = TRUE;
  }

  // With that flag we ensure we don't apply the title two times to the same
  // node. See auto_entitylabel_is_needed().
  $entity->auto_entitylabel_applied = TRUE;
}