You are here

class ButtonController in Command Buttons 7

Entity controller class.

Hierarchy

Expanded class hierarchy of ButtonController

1 string reference to 'ButtonController'
command_buttons_entity_info in ./command_buttons.module
Impliments hook_crud_hook_entity_info().

File

includes/ButtonController.class.php, line 11
Contains the controller class for the OA Button entity.

View source
class ButtonController extends EntityAPIControllerExportable {
  public $entity;
  public function access($op, $entity = NULL, $account = NULL) {
    if ($op !== 'create' && !$entity) {
      return FALSE;
    }

    // The administer permission is a blanket override.
    if (user_access('administer command buttons')) {
      return TRUE;
    }
    switch ($op) {
      case 'create':
        return user_access('create button ' . $entity);
      case 'view':
        return TRUE;
      case 'update':
        ctools_include('context');
        return user_access('edit button ' . $entity->bundle);
      case 'delete':
        ctools_include('context');
        return user_access('delete button ' . $entity->bundle);
    }
    return FALSE;
  }
  public function save($entity, DatabaseTransaction $transaction = NULL) {
    $entity = (object) $entity;

    // Determine if we will be inserting a new entity.
    $entity->is_new = !(isset($entity->bid) && is_numeric($entity->bid));
    $transaction = db_transaction();

    // Set the timestamp fields.
    if (empty($entity->created)) {
      $entity->created = REQUEST_TIME;
    }
    $entity->changed = REQUEST_TIME;
    field_attach_presave('command_button', $entity);
    module_invoke_all('entity_presave', $entity, 'command_button');
    try {
      if (!$entity->is_new) {
        drupal_write_record('command_buttons', $entity, 'bid');
        field_attach_update('command_button', $entity);
        module_invoke_all('entity_update', $entity, 'command_button');
      }
      else {
        drupal_write_record('command_buttons', $entity);
        field_attach_insert('command_button', $entity);
        module_invoke_all('entity_insert', $entity, 'command_button');
      }
      return $entity;
    } catch (Exception $e) {
      $transaction
        ->rollback();
      watchdog_exception('command_button', $e);
    }
    return FALSE;
  }
  public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
    if (!isset($langcode)) {
      $langcode = $GLOBALS['language_content']->language;
    }

    // Ensure entities is an array.
    if (!is_array($entities)) {
      $entities = array(
        $entities,
      );
    }
    $build = array();
    foreach ($entities as $entity) {

      // Populate $entity->content with a render() array.
      $this
        ->buildContent($entity, $view_mode, $langcode);

      // Allow modules to modify the structured button.
      $type = 'command_button';
      drupal_alter(array(
        'command_buttons_view',
        'entity_view',
      ), $entity->content, $type);
      $build[] = $entity->content;

      // We don't need duplicate rendering info in $entity->content.
      unset($entity->content);
    }
    return $build;
  }

  /**
   * Builds a structured array representing the button content.
   *
   * @param object $entity
   *   A button entity.
   * @param string $view_mode
   *   View mode, e.g. 'full', 'teaser'...
   * @param string $langcode
   *   (optional) A language code to use for rendering. Defaults to the global
   *   content language of the current request.
   */
  public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
    if (!isset($langcode)) {
      $langcode = $GLOBALS['language_content']->language;
    }

    // Remove previously built content, if exists.
    $entity->content = array();

    // Allow modules to change the view mode.
    $context = array(
      'entity_type' => 'command_button',
      'entity' => $entity,
      'langcode' => $langcode,
    );
    drupal_alter('entity_view_mode', $view_mode, $context);

    // Build fields content.
    field_attach_prepare_view('command_button', array(
      $entity->bid => $entity,
    ), $view_mode, $langcode);
    entity_prepare_view('command_button', array(
      $entity->bid => $entity,
    ), $langcode);
    $entity->content += field_attach_view('command_button', $entity, $view_mode, $langcode);

    // Allow modules to make their own additions to the entity.
    module_invoke_all('command_button_view', $entity, $view_mode, $langcode);
    module_invoke_all('entity_view', $entity, 'command_button', $view_mode, $langcode);

    // Make sure the current view mode is stored if no module has already
    // populated the related key.
    $entity->content += array(
      '#view_mode' => $view_mode,
    );
  }

  /**
   * Deletes the entities then rebuilds defaults if needed.
   *
   * @param $ids
   *  Can be an array of numeric BIDS, names, or combo as sutiable for load().
   */
  public function delete($ids, DatabaseTransaction $transaction = NULL) {
    $transaction = db_transaction();
    if (!empty($ids) && ($entities = command_buttons_load_multiple($ids, array()))) {
      try {
        foreach ($entities as $bid => $entity) {

          // Call the entity-specific callback (if any):
          module_invoke_all('entity_delete', $entity, 'command_button');
          field_attach_delete('command_button', $entity);
        }

        // Delete after calling hooks so that they can query entity tables as needed.
        db_delete('command_buttons')
          ->condition('bid', array_keys($entities), 'IN')
          ->execute();

        // Clear the page and block and entity_load_multiple caches.
        entity_get_controller('command_button')
          ->resetCache();
        foreach ($entities as $id => $entity) {
          if (entity_has_status($this->entityType, $entity, ENTITY_IN_CODE)) {
            entity_defaults_rebuild(array(
              $this->entityType,
            ));
            break;
          }
        }
      } catch (Exception $e) {
        $transaction
          ->rollback();
        watchdog_exception('command_button', $e);
        throw $e;
      }
    }
  }
  public function create(array $values = array()) {
    $entity = (object) array(
      'bundle' => $values['bundle'],
      'language' => LANGUAGE_NONE,
      'is_new' => TRUE,
    );

    // Ensure basic fields are defined.
    $values += array(
      'bundle' => 'button',
      'title' => '',
      'name' => '',
      'module' => 'command_buttons',
      'status' => 1,
    );

    // Apply the given values.
    foreach ($values as $key => $value) {
      $entity->{$key} = $value;
    }
    return $entity;
  }

  /**
   * Overridden to remove create/changed.
   */
  public function export($entity, $prefix = '') {
    $vars = get_object_vars($entity);
    unset($vars[$this->statusKey], $vars[$this->moduleKey], $vars['is_new']);
    if ($this->nameKey != $this->idKey) {
      unset($vars[$this->idKey]);
    }
    unset($vars['created']);
    unset($vars['changed']);
    return entity_var_json_export($vars, $prefix);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ButtonController::$entity public property
ButtonController::access public function
ButtonController::buildContent public function Builds a structured array representing the button content. Overrides EntityAPIController::buildContent
ButtonController::create public function Implements EntityAPIControllerInterface. Overrides EntityAPIController::create
ButtonController::delete public function Deletes the entities then rebuilds defaults if needed. Overrides EntityAPIControllerExportable::delete
ButtonController::export public function Overridden to remove create/changed. Overrides EntityAPIControllerExportable::export
ButtonController::save public function Overridden to care exportables that are overridden. Overrides EntityAPIControllerExportable::save
ButtonController::view public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerExportable::view
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::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
EntityAPIController::$bundleKey protected property
EntityAPIController::$cacheComplete protected property
EntityAPIController::$defaultRevisionKey protected property
EntityAPIController::deleteRevision public function Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface::deleteRevision
EntityAPIController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
EntityAPIController::query public function Builds and executes the query for loading.
EntityAPIController::renderEntityProperty protected function Renders a single entity property.
EntityAPIController::saveRevision protected function Saves an entity revision.
EntityAPIControllerExportable::$entityCacheByName protected property
EntityAPIControllerExportable::$nameKey protected property
EntityAPIControllerExportable::applyConditions protected function
EntityAPIControllerExportable::attachLoad protected function Overridden. Overrides DrupalDefaultEntityController::attachLoad
EntityAPIControllerExportable::buildQuery protected function Support loading by name key. Overrides EntityAPIController::buildQuery
EntityAPIControllerExportable::cacheGet protected function Overridden. Overrides DrupalDefaultEntityController::cacheGet
EntityAPIControllerExportable::cacheGetByName protected function Like cacheGet() but keyed by name.
EntityAPIControllerExportable::cacheSet protected function Overridden. Overrides DrupalDefaultEntityController::cacheSet
EntityAPIControllerExportable::invoke public function Overridden to care about reverted bundle entities and to skip Rules. Overrides EntityAPIController::invoke
EntityAPIControllerExportable::load public function Overridden to support passing numeric ids as well as names as $ids. Overrides EntityAPIController::load
EntityAPIControllerExportable::resetCache public function Overrides DrupalDefaultEntityController::resetCache(). Overrides EntityAPIController::resetCache
EntityAPIControllerExportable::__construct public function Overridden. Overrides EntityAPIController::__construct