You are here

RedhenGroupSelectionHandler.class.php in RedHen CRM 7

File

modules/redhen_org_group/plugins/selection/RedhenGroupSelectionHandler.class.php
View source
<?php

/**
 * RedHenGroup selection handler.
 */
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);
    }
  }

}

Classes

Namesort descending Description
RedhenGroupSelectionHandler RedHenGroup selection handler.