You are here

class EntityExampleBasicController in Examples for Developers 7

EntityExampleBasicController extends DrupalDefaultEntityController.

Our subclass of DrupalDefaultEntityController lets us add a few important create, update, and delete methods.

Hierarchy

Expanded class hierarchy of EntityExampleBasicController

Related topics

1 string reference to 'EntityExampleBasicController'
entity_example_entity_info in entity_example/entity_example.module
Implements hook_entity_info().

File

entity_example/entity_example.module, line 537
Implements the basic functionality required to create and display an entity.

View source
class EntityExampleBasicController extends DrupalDefaultEntityController implements EntityExampleBasicControllerInterface {

  /**
   * Create and return a new entity_example_basic entity.
   */
  public function create() {
    $entity = new stdClass();
    $entity->type = 'entity_example_basic';
    $entity->basic_id = 0;
    $entity->bundle_type = 'first_example_bundle';
    $entity->item_description = '';
    return $entity;
  }

  /**
   * Saves the custom fields using drupal_write_record().
   */
  public function save($entity) {

    // If our entity has no basic_id, then we need to give it a
    // time of creation.
    if (empty($entity->basic_id)) {
      $entity->created = time();
    }

    // Invoke hook_entity_presave().
    module_invoke_all('entity_presave', $entity, 'entity_example_basic');

    // The 'primary_keys' argument determines whether this will be an insert
    // or an update. So if the entity already has an ID, we'll specify
    // basic_id as the key.
    $primary_keys = $entity->basic_id ? 'basic_id' : array();

    // Write out the entity record.
    drupal_write_record('entity_example_basic', $entity, $primary_keys);

    // We're going to invoke either hook_entity_update() or
    // hook_entity_insert(), depending on whether or not this is a
    // new entity. We'll just store the name of hook_entity_insert()
    // and change it if we need to.
    $invocation = 'entity_insert';

    // Now we need to either insert or update the fields which are
    // attached to this entity. We use the same primary_keys logic
    // to determine whether to update or insert, and which hook we
    // need to invoke.
    if (empty($primary_keys)) {
      field_attach_insert('entity_example_basic', $entity);
    }
    else {
      field_attach_update('entity_example_basic', $entity);
      $invocation = 'entity_update';
    }

    // Invoke either hook_entity_update() or hook_entity_insert().
    module_invoke_all($invocation, $entity, 'entity_example_basic');
    return $entity;
  }

  /**
   * Delete a single entity.
   *
   * Really a convenience function for deleteMultiple().
   */
  public function delete($entity) {
    $this
      ->deleteMultiple(array(
      $entity,
    ));
  }

  /**
   * Delete one or more entity_example_basic entities.
   *
   * Deletion is unfortunately not supported in the base
   * DrupalDefaultEntityController class.
   *
   * @param array $entities
   *   An array of entity IDs or a single numeric ID.
   */
  public function deleteMultiple($entities) {
    $basic_ids = array();
    if (!empty($entities)) {
      $transaction = db_transaction();
      try {
        foreach ($entities as $entity) {

          // Invoke hook_entity_delete().
          module_invoke_all('entity_delete', $entity, 'entity_example_basic');
          field_attach_delete('entity_example_basic', $entity);
          $basic_ids[] = $entity->basic_id;
        }
        db_delete('entity_example_basic')
          ->condition('basic_id', $basic_ids, 'IN')
          ->execute();
      } catch (Exception $e) {
        $transaction
          ->rollback();
        watchdog_exception('entity_example', $e);
        throw $e;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::attachLoad protected function Attaches data to entities upon loading. 4
DrupalDefaultEntityController::buildQuery protected function Builds the query to load the entity. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
DrupalDefaultEntityController::load public function Implements DrupalEntityControllerInterface::load(). Overrides DrupalEntityControllerInterface::load
DrupalDefaultEntityController::resetCache public function Implements DrupalEntityControllerInterface::resetCache(). Overrides DrupalEntityControllerInterface::resetCache
DrupalDefaultEntityController::__construct public function Constructor: sets basic variables.
EntityExampleBasicController::create public function Create and return a new entity_example_basic entity. Overrides EntityExampleBasicControllerInterface::create
EntityExampleBasicController::delete public function Delete a single entity. Overrides EntityExampleBasicControllerInterface::delete
EntityExampleBasicController::deleteMultiple public function Delete one or more entity_example_basic entities.
EntityExampleBasicController::save public function Saves the custom fields using drupal_write_record(). Overrides EntityExampleBasicControllerInterface::save