diff --git a/relation_entity_collector/relation_entity_collector.module b/relation_entity_collector/relation_entity_collector.module index c8d293c..fc82887 100644 --- a/relation_entity_collector/relation_entity_collector.module +++ b/relation_entity_collector/relation_entity_collector.module @@ -155,39 +155,43 @@ function relation_entity_collector($form, &$form_state) { $relation_type_object = (!empty($relation_type)) ? relation_type_load($relation_type) : NULL; $options = array(); $all_entity_cache = (isset($form_state['all_entity_cache'])) ? $form_state['all_entity_cache'] : array(); - if ($relation_entities = & drupal_static('relation_entities', $all_entity_cache)) { - if (!isset($form_state['all_entity_cache'])) { - //required so that subsequent AJAX calls re-filter the list correctly - $form_state['all_entity_cache'] = $relation_entities; - } - foreach ($relation_entities as $entity_type => $entities) { - foreach ($entities as $entity_id => $entity) { - list( , , $entity_bundle) = entity_extract_ids($entity_type, $entity); - if (!is_null($relation_type_object)) { - $valid = FALSE; - foreach (array('source_bundles', 'target_bundles') as $property) { - foreach ($relation_type_object->$property as $allowed_bundle) { - if ($allowed_bundle == "$entity_type:$entity_bundle" || $allowed_bundle == "$entity_type:*") { - $valid = TRUE; - break; - } + if (!isset($form_state['all_entity_cache'])) { + $form_state['all_entity_cache'] = array(); + } + if ($relation_entities = drupal_static('relation_entities', array())) { + $form_state['all_entity_cache'] += $relation_entities; + } + foreach ($form_state['all_entity_cache'] as $entity_type => $entities) { + foreach ($entities as $entity_id => $entity) { + list( , , $entity_bundle) = entity_extract_ids($entity_type, $entity); + if (!is_null($relation_type_object)) { + $valid = FALSE; + foreach (array('source_bundles', 'target_bundles') as $property) { + foreach ($relation_type_object->$property as $allowed_bundle) { + if ($allowed_bundle == "$entity_type:$entity_bundle" || $allowed_bundle == "$entity_type:*") { + $valid = TRUE; + break 2; } } } - else { - $valid = TRUE; - } - if ($valid) { - $bundle_label = _relation_get_bundle_label($entity_type, $entity_bundle); - $options["$entity_type:$entity_id"] = $bundle_label . ': ' . entity_label($entity_type, $entity); - } + } + else { + $valid = TRUE; + } + + if ($valid) { + $bundle_label = _relation_get_bundle_label($entity_type, $entity_bundle); + $options["$entity_type:$entity_id"] = $bundle_label . ': ' . entity_label($entity_type, $entity); } } - asort($options); } + asort($options); $entity_key_default = (count($options) == 1) ? key($options) : ''; - - $form['relation_type'] = array( + $form['entity_picker'] = array( + '#prefix' => '', + '#suffix' => '', + ); + $form['entity_picker']['relation_type'] = array( '#prefix' => '', '#suffix' => '', '#type' => 'select', @@ -204,12 +208,8 @@ function relation_entity_collector($form, &$form_state) { ), ); if (!empty($_SESSION['relation_type']) && count($_SESSION['relation_entity_keys']) > 0) { - $form['relation_type']['#attributes'] = array('disabled' => TRUE); + $form['entity_picker']['relation_type']['#attributes'] = array('disabled' => TRUE); } - $form['entity_picker'] = array( - '#prefix' => '', - '#suffix' => '', - ); $form['entity_picker']['entity_key'] = array( '#type' => 'select', '#title' => t('Select an entity'), @@ -220,68 +220,66 @@ function relation_entity_collector($form, &$form_state) { '#description' => t('Selector shows all valid !entities loaded on this page.', array('!entities' => l(t('entities'), 'http://drupal.org/glossary#entity', array('absolute' => TRUE, 'external' => TRUE)))), ); - $form['pick'] = array( + $form['entity_picker']['pick'] = array( '#type' => 'submit', '#value' => t('Pick'), '#submit' => array('relation_entity_collector_pick'), '#ajax' => array( - 'wrapper' => 'relation_entity_collector_reload', - 'callback' => '_relation_entity_collector_ajax', + 'wrapper' => 'relation_entity_collector_picker', + 'callback' => '_relation_entity_collector_ajax_picker', ), ); - $form['reload'] = array( + $form['entity_picker']['reload'] = array( '#type' => 'fieldset', '#title' => t('Picked entities'), ); - $form['reload']['#prefix'] = ''; - $form['reload']['#suffix'] = ''; if (!empty($_SESSION['relation_entity_keys'])) { - $form['reload']['table']['#entity_collector_columns'] = array( + $form['entity_picker']['reload']['table']['#entity_collector_columns'] = array( 'weight', 'remove', ); foreach ($_SESSION['relation_entity_keys'] as $delta => $entity_key) { // The structure is (entity_type, entity_id, entity label). - $form['reload']['table']['weight'][] = array( + $form['entity_picker']['reload']['table']['weight'][] = array( '#type' => 'weight', '#delta' => count($_SESSION['relation_entity_keys']), '#default_value' => $delta, '#title_display' => 'invisible', '#title' => '', ); - $form['reload']['table']['remove'][] = array( + $form['entity_picker']['reload']['table']['remove'][] = array( '#name' => 'remove-' . $entity_key['entity_key'], '#type' => 'submit', '#value' => t('Remove'), '#entity_key' => $entity_key, '#submit' => array('relation_entity_collector_remove'), '#ajax' => array( - 'wrapper' => 'relation_entity_collector_reload', - 'callback' => '_relation_entity_collector_ajax', + 'wrapper' => 'relation_entity_collector_picker', + 'callback' => '_relation_entity_collector_ajax_picker', ), ); - $form['reload']['table']['#tree'] = TRUE; - $form['reload']['table']['#theme'] = 'relation_entity_collector_table'; + $form['entity_picker']['reload']['table']['#tree'] = TRUE; + $form['entity_picker']['reload']['table']['#theme'] = 'relation_entity_collector_table'; } if (!isset($relation_type_object) && !empty($relation_type)) { $relation_type_object = relation_type_load($relation_type); } $min_arity = isset($relation_type_object->min_arity) ? $relation_type_object->min_arity : 1; if (count($_SESSION['relation_entity_keys']) >= $min_arity) { - $form['reload']['save'] = array( + $form['entity_picker']['reload']['save'] = array( '#type' => 'submit', '#value' => t('Save relation'), '#submit' => array('relation_entity_collector_save'), ); } if (isset($_SESSION['relation_entity_keys'])) { - $form['reload']['clear'] = array( + $form['entity_picker']['reload']['clear'] = array( '#type' => 'submit', '#value' => t('Clear'), '#submit' => array('relation_entity_collector_clear'), '#ajax' => array( - 'wrapper' => 'relation_entity_collector_reload', - 'callback' => '_relation_entity_collector_ajax', + 'wrapper' => 'relation_entity_collector_picker', + 'callback' => '_relation_entity_collector_ajax_picker', ), ); } @@ -295,16 +293,6 @@ function relation_entity_collector($form, &$form_state) { } /** - * replace the Picked List section and possibly re-enable the relation type dropdown - */ -function _relation_entity_collector_ajax($form) { - $commands = array(); - $commands[] = ajax_command_replace('#relation_entity_collector_reload', render($form['reload'])); - $commands[] = ajax_command_replace('#relation_entity_collector_pick_type', render($form['relation_type'])); - return array('#type' => 'ajax', '#commands' => $commands); -} - -/** * Reload the picker when relation type changes. */ function _relation_entity_collector_ajax_picker($form, &$form_state) { @@ -368,7 +356,6 @@ function relation_entity_collector_validate($form, &$form_state) { $relation = _relation_entity_collector_get_entity($form_state['values']['relation_type'], $endpoints); $relation->in_progress = TRUE; _relation_entity_collector_endpoints_validate($relation, $form, $form_state); - field_attach_form_validate('relation', $relation, $form, $form_state); break; case t('Save relation'): _relation_entity_collector_endpoints_validate(_relation_entity_collector_get_entity(), $form, $form_state);