/** * {@inheritdoc} */ public function getBaseRouteParameters() { $parameters = parent::getBaseRouteParameters(); $base_entity_info = $this->entityManager->getDefinition($this->pluginDefinition['base_entity_type']); $parameters[$base_entity_info->getBundleEntityType()] = $this->entity->getTargetBundle(); return $parameters; }
/** * {@inheritdoc} */ public function setEntity(ConfigEntityInterface $entity) { if (parent::setEntity($entity)) { // Field storage config can also contain translatable values. Add the name // of the config as well to the list of configs for this entity. /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */ $field_storage = $this->entity->getFieldStorageDefinition(); /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type_info */ $entity_type_info = $this->entityManager->getDefinition($field_storage->getEntityTypeId()); $this->addConfigName($entity_type_info->getConfigPrefix() . '.' . $field_storage->id()); return TRUE; } return FALSE; }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $this->installEntitySchema('entity_test_rev'); $this->installEntitySchema('user'); $field_name = 'test'; $this->fieldStorage = FieldStorageConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test_rev', 'type' => 'string', 'cardinality' => 1]); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test_rev', 'bundle' => 'entity_test_rev', 'required' => TRUE]); $this->field->save(); // Create an entity with field data. $this->entity = EntityTestRev::create(['user_id' => mt_rand(1, 10), 'name' => $this->randomMachineName(), $field_name => $this->randomString()]); $this->entity->save(); }
/** * 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.'); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_values = $form_state->getValues(); $field_values = $form_values['field_storage']; // Save field cardinality. $cardinality = $field_values['cardinality']; $cardinality_number = $field_values['cardinality_number']; if ($cardinality === 'number') { $cardinality = $cardinality_number; } $field_values['cardinality'] = $cardinality; unset($field_values['container']); // Merge incoming form values into the existing field. $field_storage = $this->field->getFieldStorageDefinition(); foreach ($field_values as $key => $value) { $field_storage->set($key, $value); } // Update the field. try { $field_storage->save(); drupal_set_message($this->t('Updated field %label field settings.', array('%label' => $this->field->label()))); $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)); } } catch (\Exception $e) { drupal_set_message($this->t('Attempt to update field %label failed: %message.', array('%label' => $this->field->label(), '%message' => $e->getMessage())), 'error'); } }
/** * Tests ConfigFieldMapper::setEntity(). * * @covers ::setEntity */ public function testSetEntity() { $entity_type = $this->getMock('Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface'); $entity_type->expects($this->any())->method('getConfigPrefix')->will($this->returnValue('config_prefix')); $this->entityManager->expects($this->any())->method('getDefinition')->will($this->returnValue($entity_type)); $field_storage = $this->getMock('Drupal\\field\\FieldStorageConfigInterface'); $field_storage->expects($this->any())->method('id')->will($this->returnValue('field_storage_id')); $this->entity->expects($this->any())->method('getFieldStorageDefinition')->will($this->returnValue($field_storage)); $result = $this->configFieldMapper->setEntity($this->entity); $this->assertTrue($result); // Ensure that the configuration name was added to the mapper. $plugin_definition = $this->configFieldMapper->getPluginDefinition(); $this->assertTrue(in_array('config_prefix.field_storage_id', $plugin_definition['names'])); // Make sure setEntity() returns FALSE when called a second time. $result = $this->configFieldMapper->setEntity($this->entity); $this->assertFalse($result); }
/** * 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.'); }
/** * The _title_callback for the field settings form. * * @param \Drupal\field\FieldConfigInterface $field_config * The field. * * @return string * The label of the field. */ public function getTitle(FieldConfigInterface $field_config) { return SafeMarkup::checkPlain($field_config->label()); }
/** * The _title_callback for the field settings form. * * @param \Drupal\field\FieldConfigInterface $field_config * The field. * * @return string * The label of the field. */ public function getTitle(FieldConfigInterface $field_config) { return $field_config->label(); }