public function TripalEntityController::setTitle

3.x TripalEntityController.inc public TripalEntityController::setTitle($entity, $title = NULL)

Sets the title for an entity.

Parameters

$entity:

$title:

1 call to TripalEntityController::setTitle()
TripalEntityController::save in tripal/includes/TripalEntityController.inc
Saves a new entity.

File

tripal/includes/TripalEntityController.inc, line 105

Class

TripalEntityController
TripalEntityController extends DrupalDefaultEntityController.

Code

public function setTitle($entity, $title = NULL) {

  // If no title was supplied then we should try to generate one using the
  // default format set by admins.
  if (!$title) {
    // Load the TripalBundle entity for this TripalEntity.
    // Get the format for the title based on the bundle of the entity.
    // And then replace all the tokens with values from the entity fields.
    $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
    $title = tripal_get_title_format($bundle_entity);
    $title = tripal_replace_entity_tokens($title, $entity, $bundle_entity);
  }
  // Check if the passed alias has tokens.
  if ($title && (preg_match_all("/\[[^\]]*\]/", $title, $bundle_tokens))) {

    // Load the TripalBundle entity for this TripalEntity.
    $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));

    // And then replace all the tokens with values from the entity fields.
    $title = tripal_replace_entity_tokens($title, $entity, $bundle_entity);
  }
  // As long as we were able to determine a title, we should update it ;-).
  if ($title) {
    db_update('tripal_entity')
      ->fields(array(
        'title' => $title
      ))
      ->condition('id', $entity->id)
      ->execute();
  }
}