You are here

class RedhenGroupSelectionHandler in RedHen CRM 7

RedHenGroup selection handler.

Hierarchy

Expanded class hierarchy of RedhenGroupSelectionHandler

1 string reference to 'RedhenGroupSelectionHandler'
redhen_org_group.inc in modules/redhen_org_group/plugins/selection/redhen_org_group.inc

File

modules/redhen_org_group/plugins/selection/RedhenGroupSelectionHandler.class.php, line 7

View source
class RedhenGroupSelectionHandler implements EntityReference_SelectionHandler {

  /**
   * Override constructor to set field and instance.
   */
  protected function __construct($field, $instance) {
    $this->field = $field;
    $this->instance = $instance;
  }

  /**
   * Factory function: create a new instance of this handler for a given field.
   *
   * @string $field
   *   A field data structure.
   *
   * @return RedhenGroupSelectionHandler
   *   New instance of RedhenGroupSelectionHandler.
   */
  public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
    return new RedhenGroupSelectionHandler($field, $instance, $entity_type, $entity);
  }

  /**
   * Return a list of referencable entities.
   */
  public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    $entities = array();
    foreach ($this
      ->getContactGroups() as $org_id => $org) {
      $entities[$org->type][$org_id] = $this
        ->getLabel($org);
    }
    return $entities;
  }

  /**
   * Return all redhen_org groups that a given user belongs to.
   *
   * @return array
   *   Array of RedHen orgs.
   */
  protected function getContactGroups() {
    global $user;
    return redhen_org_group_contact_groups($user, FALSE, $this->instance['bundle']);
  }

  /**
   * Count entities that are referencable by a given field.
   */
  public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') {
    return count($this
      ->getContactGroups());
  }

  /**
   * Validate that entities can be referenced by this field.
   *
   * @return array
   *   An array of entity ids that are valid.
   */
  public function validateReferencableEntities(array $ids) {
    $ids = array();
    foreach ($this
      ->getContactGroups() as $org_id => $org) {
      $ids[] = $org_id;
    }
    return $ids;
  }

  /**
   * Give handler chance to alter the SelectQuery generated by EntityFieldQuery.
   */
  public function entityFieldQueryAlter(SelectQueryInterface $query) {
  }

  /**
   * Return the label of a given entity.
   */
  public function getLabel($entity) {
    return entity_label($this->field['settings']['target_type'], $entity);
  }

  /**
   * Generate a settings form for this handler.
   */
  public static function settingsForm($field, $instance) {
    return array();
  }

  /**
   * Implements EntityReferenceHandler::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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RedhenGroupSelectionHandler::countReferencableEntities public function Count entities that are referencable by a given field. Overrides EntityReference_SelectionHandler::countReferencableEntities
RedhenGroupSelectionHandler::entityFieldQueryAlter public function Give handler chance to alter the SelectQuery generated by EntityFieldQuery. Overrides EntityReference_SelectionHandler::entityFieldQueryAlter
RedhenGroupSelectionHandler::getContactGroups protected function Return all redhen_org groups that a given user belongs to.
RedhenGroupSelectionHandler::getInstance public static function Factory function: create a new instance of this handler for a given field. Overrides EntityReference_SelectionHandler::getInstance
RedhenGroupSelectionHandler::getLabel public function Return the label of a given entity. Overrides EntityReference_SelectionHandler::getLabel
RedhenGroupSelectionHandler::getReferencableEntities public function Return a list of referencable entities. Overrides EntityReference_SelectionHandler::getReferencableEntities
RedhenGroupSelectionHandler::settingsForm public static function Generate a settings form for this handler. Overrides EntityReference_SelectionHandler::settingsForm
RedhenGroupSelectionHandler::validateAutocompleteInput public function Implements EntityReferenceHandler::validateAutocompleteInput(). Overrides EntityReference_SelectionHandler::validateAutocompleteInput
RedhenGroupSelectionHandler::validateReferencableEntities public function Validate that entities can be referenced by this field. Overrides EntityReference_SelectionHandler::validateReferencableEntities
RedhenGroupSelectionHandler::__construct protected function Override constructor to set field and instance.