Created
December 12, 2013 01:46
-
-
Save jhedstrom/7921942 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Custom Entity Reference selection handler. | |
*/ | |
class MyModuleSelectionHandler extends OgSelectionHandler { | |
/** | |
* Overrides OgSelectionHandler::getInstance(). | |
*/ | |
public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) { | |
return new MyModuleSelectionHandler($field, $instance, $entity_type, $entity); | |
} | |
/** | |
* Implements EntityReferenceHandler::settingsForm(). | |
* | |
* Call directly from `EntityReference_SelectionHandler_Views` | |
* since we can't extend that class. | |
*/ | |
public static function settingsForm($field, $instance) { | |
$form = EntityReference_SelectionHandler_Views::settingsForm($field, $instance); | |
// Add needed parameters from the OgSelectionHandler::settingsForm(). | |
$entity_type = $field['settings']['target_type']; | |
$entity_info = entity_get_info($entity_type); | |
$options = array(); | |
foreach (og_membership_type_load() as $og_membership) { | |
$options[$og_membership->name] = $og_membership->description; | |
} | |
$form['membership_type'] = array( | |
'#type' => 'select', | |
'#title' => t('OG membership type'), | |
'#description' => t('Select the membership type that will be used for a subscribing user.'), | |
'#options' => $options, | |
'#default_value' => OG_MEMBERSHIP_TYPE_DEFAULT, | |
'#required' => TRUE, | |
); | |
// Hacky, but OG needs to know which bundles can be used, so hard code this. | |
$bundles = array(); | |
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) { | |
if (og_is_group_type($entity_type, $bundle_name)) { | |
$bundles[$bundle_name] = $bundle_info['label']; | |
} | |
} | |
if (!$bundles) { | |
$form['target_bundles'] = array( | |
'#type' => 'item', | |
'#title' => t('Target bundles'), | |
'#markup' => t('Error: The selected "Target type" %entity does not have bundles that are a group type', array('%entity' => $entity_info['label'])), | |
); | |
} | |
else { | |
$settings = $field['settings']['handler_settings']; | |
$settings += array( | |
'target_bundles' => array(), | |
); | |
$form['target_bundles'] = array( | |
'#type' => 'select', | |
'#title' => t('Target bundles'), | |
'#options' => $bundles, | |
'#default_value' => $settings['target_bundles'], | |
'#size' => 6, | |
'#multiple' => TRUE, | |
'#description' => t('The bundles of the entity type acting as group, that can be referenced. Optional, leave empty for all bundles.') | |
); | |
} | |
return $form; | |
} | |
/** | |
* Copied directly from `EntityReference_SelectionHandler_Views` since we | |
* can't extend this class. | |
*/ | |
protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) { | |
$view_name = $this->field['settings']['handler_settings']['view']['view_name']; | |
$display_name = $this->field['settings']['handler_settings']['view']['display_name']; | |
$args = $this->field['settings']['handler_settings']['view']['args']; | |
$entity_type = $this->field['settings']['target_type']; | |
// Check that the view is valid and the display still exists. | |
$this->view = views_get_view($view_name); | |
if (!$this->view || !isset($this->view->display[$display_name]) || !$this->view->access($display_name)) { | |
watchdog('entityreference', 'The view %view_name is no longer eligible for the %field_name field.', array('%view_name' => $view_name, '%field_name' => $this->instance['label']), WATCHDOG_WARNING); | |
return FALSE; | |
} | |
$this->view->set_display($display_name); | |
// Make sure the query is not cached. | |
$this->view->is_cacheable = FALSE; | |
// Pass options to the display handler to make them available later. | |
$entityreference_options = array( | |
'match' => $match, | |
'match_operator' => $match_operator, | |
'limit' => $limit, | |
'ids' => $ids, | |
); | |
$this->view->display_handler->set_option('entityreference_options', $entityreference_options); | |
return TRUE; | |
} | |
/** | |
* Implements EntityReferenceHandler::getReferencableEntities(). | |
* | |
* Copied directly from `EntityReference_SelectionHandler_Views` since we | |
* can't extend this class. | |
*/ | |
public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { | |
$display_name = $this->field['settings']['handler_settings']['view']['display_name']; | |
$args = $this->field['settings']['handler_settings']['view']['args']; | |
$result = array(); | |
if ($this->initializeView($match, $match_operator, $limit)) { | |
// Get the results. | |
$result = $this->view->execute_display($display_name, $args); | |
} | |
$return = array(); | |
if ($result) { | |
$target_type = $this->field['settings']['target_type']; | |
$entities = entity_load($target_type, array_keys($result)); | |
foreach ($entities as $entity) { | |
list($id,, $bundle) = entity_extract_ids($target_type, $entity); | |
$return[$bundle][$id] = $result[$id]; | |
} | |
} | |
return $return; | |
} | |
/** | |
* Implements EntityReferenceHandler::countReferencableEntities(). | |
*/ | |
function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') { | |
$this->getReferencableEntities($match, $match_operator); | |
return $this->view->total_items; | |
} | |
/** | |
* Copied directly from `EntityReference_SelectionHandler_Views` since we | |
* can't extend this class. | |
*/ | |
function validateReferencableEntities(array $ids) { | |
$display_name = $this->field['settings']['handler_settings']['view']['display_name']; | |
$args = $this->field['settings']['handler_settings']['view']['args']; | |
$result = array(); | |
if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) { | |
// Get the results. | |
$entities = $this->view->execute_display($display_name, $args); | |
$result = array_keys($entities); | |
} | |
return $result; | |
} | |
/** | |
* Implements EntityReferenceHandler::validateAutocompleteInput(). | |
*/ | |
public function validateAutocompleteInput($input, &$element, &$form_state, $form) { | |
return NULL; | |
} | |
/** | |
* Implements EntityReferenceHandler::getLabel(). | |
*/ | |
public function getLabel($entity) { | |
return entity_label($this->field['settings']['target_type'], $entity); | |
} | |
/** | |
* Implements EntityReferenceHandler::entityFieldQueryAlter(). | |
*/ | |
public function entityFieldQueryAlter(SelectQueryInterface $query) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment