You are here

class UnitsUnitUIController in Units of Measurement 7

Same name and namespace in other branches
  1. 7.2 includes/UnitsUnitUIController.class.inc \UnitsUnitUIController

Admin UI controller class for entity type 'units_unit'.

Hierarchy

Expanded class hierarchy of UnitsUnitUIController

1 string reference to 'UnitsUnitUIController'
units_ui_entity_info_alter in ./units_ui.module
Implements hook_entity_info_alter().

File

includes/UnitsUnitUIController.class.inc, line 11
Definition of UnitsUnitUIController class.

View source
class UnitsUnitUIController extends UnitsAbstractUIController {
  public function __construct($entity_type, $entity_info) {

    // We have to override %units_measure_machine_name placeholder in
    // $this->path with real bundle.
    parent::__construct($entity_type, $entity_info);
    $this->path = str_replace('%units_measure_machine_name', $this
      ->bundleArgument(), $this->path);
  }
  public function hook_menu() {

    // For this particular case we need to undo substitution of [bundle] to the
    // real bundle string made in __construct method of this class.
    $original_path = $this->path;
    $this->path = $this->entityInfo['admin ui']['path'];
    $items = parent::hook_menu();

    // We have to alter title for general overview page of entity admin UI,
    // to make it less confusing we need to include title of measure into it.
    $items[$this->path]['title callback'] = 'units_ui_unit_title';
    $items[$this->path]['title arguments'] = array(
      $this
        ->bundleArgumentPosition(),
    );

    // On the 'add' path, we need to provide info about the bundle of units
    // entity, so we have to slightly adjust the page callback and its
    // arguments in order to make it happen.
    $items[$this->path . '/add']['page callback'] = 'units_ui_entity_ui_get_bundle_add_form';
    $items[$this->path . '/add']['page arguments'] = array(
      $this->entityType,
      $this
        ->bundleArgumentPosition(),
    );

    // Putting back the original path once we are done.
    $this->path = $original_path;
    return $items;
  }
  public function overviewTable($conditions = array()) {

    // For better code reusage we prefer to add a condition and pass on to
    // parent's method, rather than running our own EntityFieldQuery and then
    // building the overview table.
    $conditions[$this->entityInfo['entity keys']['bundle']] = $this
      ->bundleArgument();
    $render = parent::overviewTable($conditions);
    $render['#attached']['css'][] = drupal_get_path('module', 'units_ui') . '/css/units-overview.css';
    return $render;
  }

  /**
   * Supportive function.
   *
   * Extracts bundle argument from menu path.
   */
  public function bundleArgument() {
    return arg($this
      ->bundleArgumentPosition());
  }
  public function entityFormSubmitBuildEntity($form, &$form_state) {

    // Before we handle on down the way to parent's method, we have to insert
    // the info about bundle into $form_state.
    $form_state['values'][$this->entityInfo['entity keys']['bundle']] = $this
      ->bundleArgument();
    return parent::entityFormSubmitBuildEntity($form, $form_state);
  }
  public function overviewTableHeaders($conditions, $rows, $additional_header = array()) {
    if ($this
      ->isFactorBased()) {
      $additional_header[] = array(
        'data' => t('Factor'),
        'class' => array(
          'units-overview-factor',
        ),
      );
    }
    return parent::overviewTableHeaders($conditions, $rows, $additional_header);
  }
  public function overviewTableRow($conditions, $id, $entity, $additional_cols = array()) {
    if ($this
      ->isFactorBased()) {
      $additional_cols[] = array(
        'data' => check_plain($entity->factor),
        'class' => array(
          'units-overview-factor',
        ),
      );
    }
    return parent::overviewTableRow($conditions, $id, $entity, $additional_cols);
  }

  /**
   * Supportive function.
   *
   * Based on entity info extracts menu argument position of bundle.
   */
  protected function bundleArgumentPosition() {

    // We have defined a custom property 'path bundle argument position' in
    // hook_entity_info(), of which we now take advantage.
    return $this->entityInfo['admin ui']['path bundle argument position'];
  }

  /**
   * Retrieve the bundle (measure) entity whose units are being administered.
   *
   * @return Entity
   *   Measure entity whose units are being administered
   */
  protected function getBundleEntity() {
    return units_measure_machine_name_load($this
      ->bundleArgument());
  }

  /**
   * Determine whether the currently administered measure is based on factors.
   *
   * @return bool
   *   Whether the currently administered measure is based on factors
   */
  protected function isFactorBased() {
    return $this
      ->getBundleEntity()->converter == 'linear';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::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.
UnitsUnitUIController::bundleArgument public function Supportive function.
UnitsUnitUIController::bundleArgumentPosition protected function Supportive function.
UnitsUnitUIController::entityFormSubmitBuildEntity public function Entity submit builder invoked via entity_ui_form_submit_build_entity(). Overrides EntityDefaultUIController::entityFormSubmitBuildEntity
UnitsUnitUIController::getBundleEntity protected function Retrieve the bundle (measure) entity whose units are being administered.
UnitsUnitUIController::hook_menu public function Provides definitions for implementing hook_menu(). Overrides UnitsAbstractUIController::hook_menu
UnitsUnitUIController::isFactorBased protected function Determine whether the currently administered measure is based on factors.
UnitsUnitUIController::overviewTable public function Generates the render array for a overview table for arbitrary entities matching the given conditions. Overrides EntityDefaultUIController::overviewTable
UnitsUnitUIController::overviewTableHeaders public function Generates the table headers for the overview table. Overrides EntityDefaultUIController::overviewTableHeaders
UnitsUnitUIController::overviewTableRow public function Generates the row for the passed entity and may be overridden in order to customize the rows. Overrides EntityDefaultUIController::overviewTableRow
UnitsUnitUIController::__construct public function Overrides EntityDefaultUIController::__construct