Loading entityreference_prepopulate.module +45 −0 Original line number Diff line number Diff line Loading @@ -114,6 +114,51 @@ function entityreference_prepopulate_field_attach_form($entity_type, $entity, &$ // on existing ones. $form[$field_name]['#access'] = FALSE; } elseif ($settings['action'] == 'value') { // Replace the original field by a 'value' field, hide subfields and // remove theme callbacks. if (!$ids) { // No prepopulated values have been found. Load the value from the // field. $wrapper = entity_metadata_wrapper($entity_type, $entity); $ids = $wrapper->{$field_name}->value(array('identifier' => TRUE)); if ($field['cardinality'] == 1) { $ids = array($ids); } } // Load prepopulated entities and get their labels. $entities = entity_load($field['settings']['target_type'], $ids); $entities_labels = array(); foreach ($entities as $entity_item) { $entities_labels[] = check_plain(entity_label($field['settings']['target_type'], $entity_item)); } $title = check_plain($form[$field_name][$lang]['#title']); // Define the value. $form[$field_name][$lang] = array( '#type' => 'value', '#value' => $ids, ); // Use the labels as markup in the new 'item' field. if (count($entities_labels) == 1) { $form[$field_name][$lang . '_label'] = array( '#type' => 'item', '#title' => $title, '#markup' => $entities_labels[0], ); } else { $form[$field_name][$lang . '_label'] = array( '#theme' => 'item_list', '#title' => $title, '#items' => $entities_labels, ); } } } elseif (in_array($settings['fallback'], array('form_error', 'redirect', 'hide'))) { $message = t('Field @label must be populated via URL.', array('@label' => $instance['label'])); Loading entityreference_prepopulate.test +26 −0 Original line number Diff line number Diff line Loading @@ -170,6 +170,19 @@ class EntityReferenceReferenceRemain extends DrupalWebTestCase { $this->drupalPost('node/add/' . $this->node1->type, $edit, t('Save'), $options); $this->assertText('Referenced node', 'The reference has been created'); // Transform the field in a value field and display an item field. $this->changeInstanceSettings(array('action' => 'value')); $this->drupalGet('node/add/' . $this->node1->type, $options); $xpath = $this->xpath('//input[@id="edit-node-ref-und-0-target-id"]'); $this->assertTrue(empty($xpath), 'The field is not visible to the user.'); $xpath = $this->xpath('//div[@id="edit-node-ref-und-label"]'); $this->assertTrue(!empty($xpath), 'The field has been replaced by an item field.'); $this->drupalPost('node/add/' . $this->node1->type, $edit, t('Save'), $options); $this->assertText('Referenced node', 'The reference has been created'); // Set an error when the prepopulated value is missing. $this->changeInstanceSettings(array('fallback' => 'form_error')); Loading Loading @@ -217,6 +230,19 @@ class EntityReferenceReferenceRemain extends DrupalWebTestCase { $this->drupalPost('node/' . $this->node1->nid . '/edit', array('title' => 'Referencing node'), t('Save')); $this->verifyReferenceRemain(); // Define the value option when editing. $this->changeInstanceSettings(array( 'action' => 'value', 'action_on_edit' => TRUE, )); $this->drupalGet('node/' . $this->node1->nid . '/edit'); $xpath = $this->xpath('//input[@id="edit-node-ref-und-0-target-id"]'); $this->assertTrue(empty($xpath), 'The field is not visible to the user.'); $xpath = $this->xpath('//div[@id="edit-node-ref-und-label" and contains(., "Referenced node")]'); $this->assertTrue(!empty($xpath), 'The field has been replaced by an item field.'); } /** Loading plugins/behavior/EntityReferencePrepopulateInstanceBehavior.class.php +1 −0 Original line number Diff line number Diff line Loading @@ -13,6 +13,7 @@ class EntityReferencePrepopulateInstanceBehavior extends EntityReference_Behavio 'none' => t('Do nothing'), 'hide' => t('Hide field'), 'disable' => t('Disable field'), 'value' => t('Only show entity title'), ), '#description' => t('Action to take when prepopulating field with values via URL.'), ); Loading Loading
entityreference_prepopulate.module +45 −0 Original line number Diff line number Diff line Loading @@ -114,6 +114,51 @@ function entityreference_prepopulate_field_attach_form($entity_type, $entity, &$ // on existing ones. $form[$field_name]['#access'] = FALSE; } elseif ($settings['action'] == 'value') { // Replace the original field by a 'value' field, hide subfields and // remove theme callbacks. if (!$ids) { // No prepopulated values have been found. Load the value from the // field. $wrapper = entity_metadata_wrapper($entity_type, $entity); $ids = $wrapper->{$field_name}->value(array('identifier' => TRUE)); if ($field['cardinality'] == 1) { $ids = array($ids); } } // Load prepopulated entities and get their labels. $entities = entity_load($field['settings']['target_type'], $ids); $entities_labels = array(); foreach ($entities as $entity_item) { $entities_labels[] = check_plain(entity_label($field['settings']['target_type'], $entity_item)); } $title = check_plain($form[$field_name][$lang]['#title']); // Define the value. $form[$field_name][$lang] = array( '#type' => 'value', '#value' => $ids, ); // Use the labels as markup in the new 'item' field. if (count($entities_labels) == 1) { $form[$field_name][$lang . '_label'] = array( '#type' => 'item', '#title' => $title, '#markup' => $entities_labels[0], ); } else { $form[$field_name][$lang . '_label'] = array( '#theme' => 'item_list', '#title' => $title, '#items' => $entities_labels, ); } } } elseif (in_array($settings['fallback'], array('form_error', 'redirect', 'hide'))) { $message = t('Field @label must be populated via URL.', array('@label' => $instance['label'])); Loading
entityreference_prepopulate.test +26 −0 Original line number Diff line number Diff line Loading @@ -170,6 +170,19 @@ class EntityReferenceReferenceRemain extends DrupalWebTestCase { $this->drupalPost('node/add/' . $this->node1->type, $edit, t('Save'), $options); $this->assertText('Referenced node', 'The reference has been created'); // Transform the field in a value field and display an item field. $this->changeInstanceSettings(array('action' => 'value')); $this->drupalGet('node/add/' . $this->node1->type, $options); $xpath = $this->xpath('//input[@id="edit-node-ref-und-0-target-id"]'); $this->assertTrue(empty($xpath), 'The field is not visible to the user.'); $xpath = $this->xpath('//div[@id="edit-node-ref-und-label"]'); $this->assertTrue(!empty($xpath), 'The field has been replaced by an item field.'); $this->drupalPost('node/add/' . $this->node1->type, $edit, t('Save'), $options); $this->assertText('Referenced node', 'The reference has been created'); // Set an error when the prepopulated value is missing. $this->changeInstanceSettings(array('fallback' => 'form_error')); Loading Loading @@ -217,6 +230,19 @@ class EntityReferenceReferenceRemain extends DrupalWebTestCase { $this->drupalPost('node/' . $this->node1->nid . '/edit', array('title' => 'Referencing node'), t('Save')); $this->verifyReferenceRemain(); // Define the value option when editing. $this->changeInstanceSettings(array( 'action' => 'value', 'action_on_edit' => TRUE, )); $this->drupalGet('node/' . $this->node1->nid . '/edit'); $xpath = $this->xpath('//input[@id="edit-node-ref-und-0-target-id"]'); $this->assertTrue(empty($xpath), 'The field is not visible to the user.'); $xpath = $this->xpath('//div[@id="edit-node-ref-und-label" and contains(., "Referenced node")]'); $this->assertTrue(!empty($xpath), 'The field has been replaced by an item field.'); } /** Loading
plugins/behavior/EntityReferencePrepopulateInstanceBehavior.class.php +1 −0 Original line number Diff line number Diff line Loading @@ -13,6 +13,7 @@ class EntityReferencePrepopulateInstanceBehavior extends EntityReference_Behavio 'none' => t('Do nothing'), 'hide' => t('Hide field'), 'disable' => t('Disable field'), 'value' => t('Only show entity title'), ), '#description' => t('Action to take when prepopulating field with values via URL.'), ); Loading