You are here

class OaTeams_SelectionHandler in Open Atrium Core 7.2

An EntityReference selection plugin for Open Atrium Teams.

It show the appropriate list of teams for the current Space, including inherited Teams.

Hierarchy

Expanded class hierarchy of OaTeams_SelectionHandler

1 string reference to 'OaTeams_SelectionHandler'
oa_teams.inc in modules/oa_teams/plugins/entityreference_selection/oa_teams.inc

File

modules/oa_teams/plugins/entityreference_selection/OaTeams_SelectionHandler.class.php, line 13
Contains the OaTeams_SelectionHandler class.

View source
class OaTeams_SelectionHandler implements EntityReference_SelectionHandler {

  /**
   * Implements EntityReferenceHandler::getInstance().
   */
  public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
    if ($field['settings']['target_type'] !== 'node') {
      return EntityReference_SelectionHandler_Broken::getInstance($field, $instance);
    }
    return new OaTeams_SelectionHandler();
  }
  protected function __construct() {

    // We don't need to construct anything!
  }

  /**
   * Implements EntityReferenceHandler::settingsForm().
   */
  public static function settingsForm($field, $instance) {
    return array();
  }

  /**
   * Implements EntityReferenceHandler::getReferencableEntities().
   */
  public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    $nids = array_keys(oa_teams_get_teams_for_space());
    $teams = node_load_multiple($nids);
    $options = array(
      OA_TEAM_TYPE => array(),
    );
    $count = 0;
    foreach ($teams as $nid => $team) {
      $count++;
      $label = $this
        ->getLabel($team);
      if (!$match || stripos($label, $match) !== FALSE) {
        $options[OA_TEAM_TYPE][$nid] = check_plain($this
          ->getLabel($team));
      }
      if ($limit && $count == $limit) {
        break;
      }
    }
    return $options;
  }

  /**
   * Implements EntityReferenceHandler::countReferencableEntities().
   */
  public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') {
    $options = $this
      ->getReferencableEntities();
    return count($options['node']);
  }

  /**
   * Implements EntityReferenceHandler::validateReferencableEntities().
   */
  public function validateReferencableEntities(array $ids) {

    // Assume any are valid.
    $validated = $ids;
    return $validated;
  }

  /**
   * Implements EntityReferenceHandler::validateAutocompleteInput().
   *
   * Copied from EntityReference_SelectionHandler_Generic::validateAutocompleteInput().
   */
  public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
    $entities = $this
      ->getReferencableEntities($input, '=', 6);
    if (empty($entities)) {

      // Error if there are no entities available for a required field.
      form_error($element, t('There are no entities matching "%value"', array(
        '%value' => $input,
      )));
    }
    elseif (count($entities) > 5) {

      // Error if there are more than 5 matching entities.
      form_error($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)"', array(
        '%value' => $input,
        '@value' => $input,
        '@id' => key($entities),
      )));
    }
    elseif (count($entities) > 1) {

      // More helpful error if there are only a few matching entities.
      $multiples = array();
      foreach ($entities as $id => $name) {
        $multiples[] = $name . ' (' . $id . ')';
      }
      form_error($element, t('Multiple entities match this reference; "%multiple"', array(
        '%multiple' => implode('", "', $multiples),
      )));
    }
    else {

      // Take the one and only matching entity.
      return key($entities);
    }
  }

  /**
   * Implements EntityReferenceHandler::entityFieldQueryAlter().
   */
  public function entityFieldQueryAlter(SelectQueryInterface $query) {
  }

  /**
   * Implements EntityReferenceHandler::getLabel().
   */
  public function getLabel($entity) {
    return entity_access('view', 'node', $entity) ? entity_label('node', $entity) : t('- Restricted access -');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OaTeams_SelectionHandler::countReferencableEntities public function Implements EntityReferenceHandler::countReferencableEntities(). Overrides EntityReference_SelectionHandler::countReferencableEntities
OaTeams_SelectionHandler::entityFieldQueryAlter public function Implements EntityReferenceHandler::entityFieldQueryAlter(). Overrides EntityReference_SelectionHandler::entityFieldQueryAlter
OaTeams_SelectionHandler::getInstance public static function Implements EntityReferenceHandler::getInstance(). Overrides EntityReference_SelectionHandler::getInstance
OaTeams_SelectionHandler::getLabel public function Implements EntityReferenceHandler::getLabel(). Overrides EntityReference_SelectionHandler::getLabel
OaTeams_SelectionHandler::getReferencableEntities public function Implements EntityReferenceHandler::getReferencableEntities(). Overrides EntityReference_SelectionHandler::getReferencableEntities
OaTeams_SelectionHandler::settingsForm public static function Implements EntityReferenceHandler::settingsForm(). Overrides EntityReference_SelectionHandler::settingsForm
OaTeams_SelectionHandler::validateAutocompleteInput public function Implements EntityReferenceHandler::validateAutocompleteInput(). Overrides EntityReference_SelectionHandler::validateAutocompleteInput
OaTeams_SelectionHandler::validateReferencableEntities public function Implements EntityReferenceHandler::validateReferencableEntities(). Overrides EntityReference_SelectionHandler::validateReferencableEntities
OaTeams_SelectionHandler::__construct protected function