summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjastraat2013-04-12 19:08:38 (GMT)
committer Amitai Burstein2013-04-12 19:08:38 (GMT)
commit302f8c1372156296098502bb7ae4ce76eb9d03da (patch)
treeec1ee6c91f790f5debb9d1697d2efcade742e774
parentc1e10fad0e033f1256d0095328d4af9740740662 (diff)
Issue #1958800 by jastraat, Amitaibu: Better integration for OG's "create" permissions
-rw-r--r--entityreference_prepopulate.module68
1 files changed, 35 insertions, 33 deletions
diff --git a/entityreference_prepopulate.module b/entityreference_prepopulate.module
index c8e88a1..bc9ccd6 100644
--- a/entityreference_prepopulate.module
+++ b/entityreference_prepopulate.module
@@ -131,7 +131,7 @@ function entityreference_prepopulate_field_attach_form($entity_type, $entity, &$
// Store prepopulated values in the form state to make them persistent,
// in case the form is rebuilt by AJAX requests.
- if ($values = entityreference_prepopulate_get_values($field, $instance, TRUE)) {
+ if ($values = entityreference_prepopulate_get_values($field, $instance)) {
$form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field['field_name']] = $values;
}
@@ -208,17 +208,17 @@ function entityreference_prepopulate_field_access($op, $field, $entity_type, $en
* TRUE if the group IDs should be sent as a simple array. FALSE, if we
* need to build array suited for field API's $items value.
*/
-function entityreference_prepopulate_get_values($field, $instance, $flat_array = FALSE, $validate = TRUE) {
+function entityreference_prepopulate_get_values($field, $instance, $flat_array = FALSE) {
$settings = $instance['settings']['behaviors']['prepopulate'];
if (!empty($settings['og_context']) && module_exists('og_context')) {
- return entityreference_prepopulate_get_values_from_og_context($field, $instance, $flat_array, $validate);
+ return entityreference_prepopulate_get_values_from_og_context($field, $instance, $flat_array);
}
- return entityreference_prepopulate_get_values_from_url($field, $instance, $flat_array, $validate);
+ return entityreference_prepopulate_get_values_from_url($field, $instance, $flat_array);
}
-function entityreference_prepopulate_get_values_from_og_context($field, $instance, $flat_array = FALSE, $validate = TRUE) {
+function entityreference_prepopulate_get_values_from_og_context($field, $instance, $flat_array = FALSE) {
$field_name = $field['field_name'];
if (!og_is_group_audience_field($field_name) || !$og_context = og_context()) {
@@ -249,7 +249,7 @@ function entityreference_prepopulate_get_values_from_og_context($field, $instanc
* @see
* entityreference_prepopulate_get_values()
*/
-function entityreference_prepopulate_get_values_from_url($field, $instance, $flat_array = FALSE, $validate = TRUE) {
+function entityreference_prepopulate_get_values_from_url($field, $instance, $flat_array = FALSE) {
$cache = &drupal_static(__FUNCTION__, array());
$field_name = $field['field_name'];
$identifier = $instance['entity_type'] . ':' . $instance['bundle'] . ':' . $field_name . ':' . $flat_array;
@@ -263,37 +263,18 @@ function entityreference_prepopulate_get_values_from_url($field, $instance, $fla
return;
}
- // Get the value from the URL if possible.
- if (!empty($_GET[$field_name]) && is_string($_GET[$field_name])) {
- $ids = explode(',', $_GET[$field_name]);
- }
- else {
- // Try to get the form out of cache.
- $form_build_id = isset($_GET['form_build_id']) ? $_GET['form_build_id'] : isset($_POST['form_build_id']) ? $_POST['form_build_id'] : NULL;
- $form_state = array();
- $form = form_get_cache($form_build_id, $form_state);
-
- // If successful, get the value from the form_state.
- if (isset($form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name])) {
- $ids = $form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name];
- }
- // If not, do nothing and return.
- else {
- $cache[$identifier] = FALSE;
- return;
- }
+ if (!$ids = entityreference_prepopulate_get_url_or_cached_values($field_name, $instance)) {
+ $cache[$identifier] = FALSE;
+ return;
}
- // Check if the IDs are valid, and get filter out the ones that are not valid.
- if ($validate) {
- $handler = entityreference_get_selection_handler($field, $instance);
- if (!$ids = $handler->validateReferencableEntities($ids)) {
- $cache[$identifier] = FALSE;
- return;
- }
+ // Check if the IDs are valid, and filter out the invalid ones.
+ $handler = entityreference_get_selection_handler($field, $instance);
+ if (!$ids = $handler->validateReferencableEntities($ids)) {
+ $cache[$identifier] = FALSE;
+ return;
}
-
// Check access to the provided entities.
$target_type = $field['settings']['target_type'];
entity_load($target_type, $ids);
@@ -308,6 +289,27 @@ function entityreference_prepopulate_get_values_from_url($field, $instance, $fla
return $items;
}
+
+/**
+ * Helper function to get the values from the URL or cached form.
+ *
+ * @param $field_name
+ * The field name.
+ */
+function entityreference_prepopulate_get_url_or_cached_values($field_name, $instance) {
+ // Get the value from the URL if possible.
+ if (!empty($_GET[$field_name]) && is_string($_GET[$field_name])) {
+ return explode(',', $_GET[$field_name]);
+ }
+ // Try to get the form out of cache.
+ $form_build_id = isset($_GET['form_build_id']) ? $_GET['form_build_id'] : isset($_POST['form_build_id']) ? $_POST['form_build_id'] : NULL;
+ $form_state = array();
+ $form = form_get_cache($form_build_id, $form_state);
+
+ // If successful, get the value from the form_state.
+ return isset($form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name]) ? isset($form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name]) : FALSE;
+}
+
/**
* Return a form element with crafted links to create nodes for a group.
*