You are here

class ClientsHandlerEntityUIController in Web Service Clients 7.3

Default controller for providing handler UI.

Expects:

  • 'type' key on the entity to be the handler type.
  • various entity info property documented in clients_hook_entity_info().

Hierarchy

Expanded class hierarchy of ClientsHandlerEntityUIController

File

includes/clients.ui.inc, line 15
Provides a controller for building an entity overview form.

View source
class ClientsHandlerEntityUIController extends EntityDefaultUIController {

  /**
   * Provides definitions for implementing hook_menu().
   */
  public function hook_menu() {

    // Start with what EntityAPI gives us, then doctor it.
    $items = parent::hook_menu();

    // Change the EntityAPI to deal with multiple handler types.
    $items[$this->path . '/add']['title'] = 'Add TODO';
    $items[$this->path . '/add']['page callback'] = 'clients_handler_add_page';
    $items[$this->path . '/add']['page arguments'] = array(
      $this->entityType,
    );

    // Add links. Follow same pattern as node.
    $handler_types_callback = $this->entityInfo['admin ui']['types callback'];
    $handler_types = $handler_types_callback();
    foreach ($handler_types as $type => $type_info) {
      $items[$this->path . '/add/' . $type] = array(
        'title' => 'Create !name',
        'title arguments' => array(
          '!name' => $type_info['label'],
        ),
        'page callback' => 'clients_handler_get_add_form',
        'page arguments' => array(
          $this->entityType,
          $type,
        ),
        'file' => $this->entityInfo['admin ui']['file'],
        // Need to specify file path as this gets used in entity_menu().
        'file path' => drupal_get_path('module', $this->entityInfo['module']),
      );
    }

    // All menu items for handlers have the same access.
    // This means we don't need an access callback.
    foreach ($items as $path => $item) {
      $items[$path]['access callback'] = 'user_access';
      $items[$path]['access arguments'] = array(
        $this->entityInfo['admin ui']['access permission'],
      );
    }
    return $items;
  }

  /**
   * Overriden to sort the handlers by machine name.
   *
   * @param $conditions
   *   An array of conditions as needed by entity_load().
   *
   * @return Array
   *   A renderable array.
   */
  public function overviewTable($conditions = array()) {
    $entities = $this
      ->getOverviewEntities($conditions);
    $rows = array();
    foreach ($entities as $entity) {
      $rows[] = $this
        ->overviewTableRow($conditions, entity_id($this->entityType, $entity), $entity);
    }
    $render = array(
      '#theme' => 'table',
      '#header' => $this
        ->overviewTableHeaders($conditions, $rows),
      '#rows' => $rows,
      '#empty' => t('None.'),
    );
    return $render;
  }

  /**
   * Retrieves the entities for the admin overview.
   */
  function getOverviewEntities($conditions) {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', $this->entityType);

    // Add all conditions to query.
    foreach ($conditions as $key => $value) {
      $query
        ->propertyCondition($key, $value);
    }

    // Add the sort order.
    // entity_load() appears to respect the order of the incoming $ids array,
    // so no need to do any sorting here.
    $query
      ->propertyOrderBy('name');
    if ($this->overviewPagerLimit) {
      $query
        ->pager($this->overviewPagerLimit);
    }
    $results = $query
      ->execute();
    $ids = isset($results[$this->entityType]) ? array_keys($results[$this->entityType]) : array();
    $entities = $ids ? entity_load_multiple_by_name($this->entityType, $ids) : array();
    return $entities;
  }

  /**
   * Generates the table headers for the overview table.
   */
  protected function overviewTableHeaders($conditions, $rows, $additional_header = array()) {

    // Our subclasses have almost certainly added additional columns.
    // Put the handler type at the front so it comes just after the name.
    array_unshift($additional_header, t('Type'));
    return parent::overviewTableHeaders($conditions, $rows, $additional_header);
  }

  /**
   * Generates the row for the passed entity and may be overridden in order to
   * customize the rows.
   *
   * @param $additional_cols
   *   Additional columns to be added after the entity label column.
   */
  protected function overviewTableRow($conditions, $id, $entity, $additional_cols = array()) {
    $handler_types_callback = $this->entityInfo['admin ui']['types callback'];
    $handler_types = $handler_types_callback();
    $broken_class = $this->entityInfo['factory']['broken class'];
    if (get_class($entity) == $broken_class) {

      // If the handler is broken, there will be no type data for it.
      $type_label = $entity->type;
      $type_label .= ' <span class="warning">' . t("Broken handler") . '</span>';
    }
    else {
      $handler_type = $handler_types[$entity->type];
      $type_label = $handler_type['label'];
    }

    // Our subclasses have almost certainly added additional columns.
    // Put the handler type at the front so it comes just after the name.
    array_unshift($additional_cols, $type_label);
    $row = parent::overviewTableRow($conditions, $id, $entity, $additional_cols);
    return $row;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClientsHandlerEntityUIController::getOverviewEntities function Retrieves the entities for the admin overview. 1
ClientsHandlerEntityUIController::hook_menu public function Provides definitions for implementing hook_menu(). Overrides EntityDefaultUIController::hook_menu 2
ClientsHandlerEntityUIController::overviewTable public function Overriden to sort the handlers by machine name. Overrides EntityDefaultUIController::overviewTable
ClientsHandlerEntityUIController::overviewTableHeaders protected function Generates the table headers for the overview table. Overrides EntityDefaultUIController::overviewTableHeaders 2
ClientsHandlerEntityUIController::overviewTableRow protected function Generates the row for the passed entity and may be overridden in order to customize the rows. Overrides EntityDefaultUIController::overviewTableRow 2
EntityDefaultUIController::$entityInfo protected property
EntityDefaultUIController::$entityType protected property
EntityDefaultUIController::$id_count protected property
EntityDefaultUIController::$overviewPagerLimit public property Defines the number of entries to show per page in overview table.
EntityDefaultUIController::applyOperation public function Applies an operation to the given entity.
EntityDefaultUIController::entityFormSubmitBuildEntity public function Entity submit builder invoked via entity_ui_form_submit_build_entity().
EntityDefaultUIController::hook_forms public function Provides definitions for implementing hook_forms().
EntityDefaultUIController::operationCount protected function Returns the operation count for calculating colspans.
EntityDefaultUIController::operationForm public function Builds the operation form.
EntityDefaultUIController::operationFormSubmit public function Operation form submit callback. 1
EntityDefaultUIController::operationFormValidate public function Operation form validation callback.
EntityDefaultUIController::overviewForm public function Builds the entity overview form.
EntityDefaultUIController::overviewFormSubmit public function Overview form submit callback.
EntityDefaultUIController::overviewFormValidate public function Overview form validation callback.
EntityDefaultUIController::__construct public function