diff --git a/inline_entity_form.info b/inline_entity_form.info index 85060a1..07ddcd5 100644 --- a/inline_entity_form.info +++ b/inline_entity_form.info @@ -8,3 +8,6 @@ files[] = includes/entity.inline_entity_form.inc files[] = includes/node.inline_entity_form.inc files[] = includes/commerce_product.inline_entity_form.inc files[] = includes/commerce_line_item.inline_entity_form.inc + +; Tests +files[] = inline_entity_form.test diff --git a/inline_entity_form.module b/inline_entity_form.module index ecfe927..095a083 100644 --- a/inline_entity_form.module +++ b/inline_entity_form.module @@ -224,6 +224,9 @@ function inline_entity_form_field_widget_form(&$form, &$form_state, $field, $ins // Add entity type specific CSS. _inline_entity_form_attach_css($controller->css(), $element['#attached']['css']); + // Add a validator for setting the inline entities language. + $form['#validate'][] = 'inline_entity_form_update_language_validate'; + // Initialize the IEF array in form state. if (empty($form_state['inline_entity_form'][$ief_id])) { $form_state['inline_entity_form'][$ief_id] = array( @@ -471,6 +474,32 @@ function inline_entity_form_field_widget_form(&$form, &$form_state, $field, $ins } /** + * Validation callback for forms holding an inline entity form. + * + * Sets the inline entities language to the language selected on the form. + */ +function inline_entity_form_update_language_validate(&$form, &$form_state) { + // Ignore the procedure if there was no language selection or if the neutral + // language was selected. + if (empty($form_state['values']['language']) || $form_state['values']['language'] == LANGUAGE_NONE) { + return; + } + + // Iterate the inline entities and set their language. + foreach ($form_state['inline_entity_form'] as &$ief) { + foreach ($ief['entities'] as &$entity) { + // Ignore entities which already have some language defined. + if (!empty($entity['entity']->language) && $entity['entity']->language != LANGUAGE_NONE) { + continue; + } + + $entity['entity']->language = $form_state['values']['language']; + $entity['needs_save'] = TRUE; + } + } +} + +/** * Wraps and returns the entity form provided by the passed-in controller. * * @param $controller diff --git a/inline_entity_form.test b/inline_entity_form.test new file mode 100644 index 0000000..dcc953b --- /dev/null +++ b/inline_entity_form.test @@ -0,0 +1,134 @@ + 'Inline Entity Form Locale', + 'description' => 'Test the Inline entity form language handling.', + 'group' => 'Inline Entity Form', + ); + } + + function setUp() { + parent::setUp('inline_entity_form', 'entityreference', 'locale'); + + $account = $this->drupalCreateUser(array('bypass node access')); + $this->drupalLogin($account); + } + + /** + * Test inline entity form locale. + * + * Make sure child entities inherit the language from the parent. + */ + function testInlineEntityFormLocale() { + // Add a language. + locale_add_language('l0', 'Language0', 'Language0'); + + // Create the parent type. + $this->createNodeType('parent'); + $this->createNodeType('child'); + $this->addParentChildReferenceField(); + variable_set('language_content_type_parent', 2); + + + // 1. Create a parent and child nodes with a specific language, make sure + // the child node gets the language. + $edit = array( + 'title' => $this->randomName(10), + 'node_ref[' . LANGUAGE_NONE . '][form][title]' => $this->randomName(10), + 'language' => 'l0', + ); + $this->drupalPost('node/add/parent', $edit, t('Save')); + // Load the "child" node. + $nodes = node_load_multiple(array(), array('type' => 'child')); + $this->assertTrue(count($nodes) == 1, t('A child node was created by the inline form.')); + $l0_child_node = reset($nodes); + $this->assertTrue($l0_child_node->language == 'l0', t('The inline node language was set correctly.')); + + + // 2. Create a parent and child nodes with the neutral language, make sure + // the child gets the neutral language. + $edit = array( + 'title' => $this->randomName(10), + 'node_ref[' . LANGUAGE_NONE . '][form][title]' => $this->randomName(10), + ); + $this->drupalPost('node/add/parent', $edit, t('Save')); + // Load the new "child" node. + $nodes = node_load_multiple(array(), array('type' => 'child')); + $neutral_child_node = end($nodes); + $this->assertTrue($neutral_child_node->language == LANGUAGE_NONE, t('The inline node language was set to neutral.')); + + + // 3. Edit a neutral language parent's language and make sure the child's + // language is updated. + // Load the parent with the neutral language. + $nodes = node_load_multiple(array(), array('type' => 'parent')); + $neutral_parent_node = end($nodes); + // Update the parent node language. + $edit = array('language' => 'l0'); + $this->drupalPost('node/' . $neutral_parent_node->nid . '/edit', $edit, t('Save')); + // Reload the former neutral language child (A reset is needed to see the + // updated language). + $nodes = node_load_multiple(array($neutral_child_node->nid), array(), TRUE); + $neutral_child_node = reset($nodes); + $this->assertTrue($neutral_child_node->language == 'l0', t('The inline node language was updated as the parent was edited.')); + } + + /** + * Create a node type. + * + * @param $bundle_name + * The bundle name. + */ + private function createNodeType($bundle_name) { + $content_type = array( + 'type' => $bundle_name, + 'name' => $bundle_name, + 'base' => 'node_content', + 'mode' => TRANSLATION_ENABLED, + ); + + $content_type = node_type_set_defaults($content_type); + node_type_save($content_type); + } + + /** + * Create an entity-reference field from the parent type to the child type. + */ + private function addParentChildReferenceField() { + // Create an entityreference field. + $field = array( + 'translatable' => FALSE, + 'entity_types' => array('node'), + 'settings' => array( + 'handler' => 'base', + 'target_type' => 'node', + 'handler_settings' => array( + 'target_bundles' => array('child'), + ), + ), + 'field_name' => 'node_ref', + 'type' => 'entityreference', + 'cardinality' => 1, + ); + field_create_field($field); + + $instance = array( + 'field_name' => 'node_ref', + 'entity_type' => 'node', + 'label' => 'Child', + 'bundle' => 'parent', + 'description' => 'Node reference.', + 'required' => TRUE, + 'widget' => array( + 'type' => 'inline_entity_form', + 'module' => 'inline_entity_form', + ), + ); + field_create_instance($instance); + } +}