/**
  * Tests that changing the country clears the expected values.
  */
 function testClearValues()
 {
     $field_name = $this->field->getName();
     // Create an article with all fields filled.
     $edit = [];
     $edit['title[0][value]'] = $this->randomMachineName(8);
     $edit[$field_name . '[0][country_code]'] = 'US';
     $edit[$field_name . '[0][recipient]'] = 'Some Recipient';
     $edit[$field_name . '[0][organization]'] = 'Some Organization';
     $edit[$field_name . '[0][address_line1]'] = '1098 Alta Ave';
     $edit[$field_name . '[0][address_line2]'] = 'Street 2';
     $edit[$field_name . '[0][locality]'] = 'Mountain View';
     $edit[$field_name . '[0][administrative_area]'] = 'US-CA';
     $edit[$field_name . '[0][postal_code]'] = '94043';
     $this->drupalPostForm($this->nodeAddUrl, $edit, t('Save'));
     $this->assertResponse(200);
     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
     $this->drupalGet('node/' . $node->id() . '/edit');
     $this->assertFieldByName($field_name . '[0][country_code]', 'US');
     $this->assertFieldByName($field_name . '[0][administrative_area]', 'US-CA');
     $this->assertFieldByName($field_name . '[0][locality]', 'Mountain View');
     $this->assertFieldByName($field_name . '[0][postal_code]', '94043');
     // Now change the country to China, subdivision fields should be cleared.
     $edit = [];
     $edit[$field_name . '[0][country_code]'] = 'CN';
     $this->drupalPostAjaxForm('node/' . $node->id() . '/edit', $edit, $field_name . '[0][country_code]');
     $this->assertResponse(200);
     // Check that values are cleared.
     $this->assertFieldByName($field_name . '[0][country_code]', 'CN', 'Country changed to CN');
     $this->assertFieldByName($field_name . '[0][administrative_area]', '', 'Field administrative_area has been cleared');
     $this->assertFieldByName($field_name . '[0][locality]', '', 'Field locality has been cleared');
     $this->assertFieldByName($field_name . '[0][dependent_locality]', '', 'Field dependent_locality has been cleared');
     $this->assertFieldByName($field_name . '[0][postal_code]', '', 'Field postal_code has been cleared.');
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     parent::submitForm($form, $form_state);
     // Handle the default value.
     $default_value = array();
     if (isset($form['default_value'])) {
         $items = $form['#entity']->get($this->entity->getName());
         $default_value = $items->defaultValuesFormSubmit($form['default_value'], $form, $form_state);
     }
     $this->entity->default_value = $default_value;
 }
Example #3
0
 /**
  * Tests that editing and saving a node with no changes works correctly.
  */
 function testReSavingTags()
 {
     // Enable tags in the vocabulary.
     $field = $this->field;
     entity_get_form_display($field->entity_type, $field->bundle, 'default')->setComponent($field->getName(), array('type' => 'taxonomy_autocomplete'))->save();
     // Create a term and a node using it.
     $term = $this->createTerm($this->vocabulary);
     $edit = array();
     $edit['title[0][value]'] = $this->randomMachineName(8);
     $edit['body[0][value]'] = $this->randomMachineName(16);
     $edit[$this->field->getName()] = $term->getName();
     $this->drupalPostForm('node/add/article', $edit, t('Save'));
     // Check that the term is displayed when editing and saving the node with no
     // changes.
     $this->clickLink(t('Edit'));
     $this->assertRaw($term->getName(), 'Term is displayed when editing the node.');
     $this->drupalPostForm(NULL, array(), t('Save'));
     $this->assertRaw($term->getName(), 'Term is displayed after saving the node with no changes.');
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     // Handle the default value.
     $default_value = array();
     if (isset($form['field']['default_value'])) {
         $items = $form['#entity']->get($this->field->getName());
         $default_value = $items->defaultValuesFormSubmit($form['field']['default_value'], $form, $form_state);
     }
     $this->field->default_value = $default_value;
     // Merge incoming values into the field.
     foreach ($form_state->getValue('field') as $key => $value) {
         $this->field->set($key, $value);
     }
     $this->field->save();
     drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->field->getLabel())));
     $request = $this->getRequest();
     if (($destinations = $request->query->get('destinations')) && ($next_destination = FieldUI::getNextDestination($destinations))) {
         $request->query->remove('destinations');
         $form_state->setRedirectUrl($next_destination);
     } else {
         $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($this->field->entity_type, $this->field->bundle));
     }
 }