/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->drupalPlaceBlock('local_tasks_block'); $this->drupalPlaceBlock('local_actions_block'); $this->drupalPlaceBlock('page_title_block'); $this->type = $this->createProfileType('test', 'Test profile', TRUE); $id = $this->type->id(); $field_storage = FieldStorageConfig::create(['field_name' => 'profile_fullname', 'entity_type' => 'profile', 'type' => 'text']); $field_storage->save(); $this->field = FieldConfig::create(['field_storage' => $field_storage, 'bundle' => $this->type->id(), 'label' => 'Full name']); $this->field->save(); // Configure the default display. $this->display = EntityViewDisplay::load("profile.{$this->type->id()}.default"); if (!$this->display) { $this->display = EntityViewDisplay::create(['targetEntityType' => 'profile', 'bundle' => $this->type->id(), 'mode' => 'default', 'status' => TRUE]); $this->display->save(); } $this->display->setComponent($this->field->getName(), ['type' => 'string'])->save(); // Configure rhe default form. $this->form = EntityFormDisplay::load("profile.{$this->type->id()}.default"); if (!$this->form) { $this->form = EntityFormDisplay::create(['targetEntityType' => 'profile', 'bundle' => $this->type->id(), 'mode' => 'default', 'status' => TRUE]); $this->form->save(); } $this->form->setComponent($this->field->getName(), ['type' => 'string_textfield'])->save(); $this->checkPermissions(['administer profile types', "view own {$id} profile", "view any {$id} profile", "add own {$id} profile", "add any {$id} profile", "edit own {$id} profile", "edit any {$id} profile", "delete own {$id} profile", "delete any {$id} profile"]); user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ['access user profiles']); $this->adminUser = $this->drupalCreateUser(['administer profile types', "view any {$id} profile", "add any {$id} profile", "edit any {$id} profile", "delete any {$id} profile"]); }
/** * Tests the dependency between ImageStyle and entity display components. */ public function testEntityDisplayDependency() { // Create two image styles. /** @var \Drupal\image\ImageStyleInterface $style */ $style = ImageStyle::create(['name' => 'main_style']); $style->save(); /** @var \Drupal\image\ImageStyleInterface $replacement */ $replacement = ImageStyle::create(['name' => 'replacement_style']); $replacement->save(); // Create a node-type, named 'note'. $node_type = NodeType::create(['type' => 'note']); $node_type->save(); // Create an image field and attach it to the 'note' node-type. FieldStorageConfig::create(['entity_type' => 'node', 'field_name' => 'sticker', 'type' => 'image'])->save(); FieldConfig::create(['entity_type' => 'node', 'field_name' => 'sticker', 'bundle' => 'note'])->save(); // Create the default entity view display and set the 'sticker' field to use // the 'main_style' images style in formatter. /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */ $view_display = EntityViewDisplay::create(['targetEntityType' => 'node', 'bundle' => 'note', 'mode' => 'default', 'status' => TRUE])->setComponent('sticker', ['settings' => ['image_style' => 'main_style']]); $view_display->save(); // Create the default entity form display and set the 'sticker' field to use // the 'main_style' images style in the widget. /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ $form_display = EntityFormDisplay::create(['targetEntityType' => 'node', 'bundle' => 'note', 'mode' => 'default', 'status' => TRUE])->setComponent('sticker', ['settings' => ['preview_image_style' => 'main_style']]); $form_display->save(); // Check that the entity displays exists before dependency removal. $this->assertNotNull(EntityViewDisplay::load($view_display->id())); $this->assertNotNull(EntityFormDisplay::load($form_display->id())); // Delete the 'main_style' image style. Before that, emulate the UI process // of selecting a replacement style by setting the replacement image style // ID in the image style storage. /** @var \Drupal\image\ImageStyleStorageInterface $storage */ $storage = $this->container->get('entity.manager')->getStorage($style->getEntityTypeId()); $storage->setReplacementId('main_style', 'replacement_style'); $style->delete(); // Check that the entity displays exists after dependency removal. $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id())); $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id())); // Check that the 'sticker' formatter component exists in both displays. $this->assertNotNull($formatter = $view_display->getComponent('sticker')); $this->assertNotNull($widget = $form_display->getComponent('sticker')); // Check that both displays are using now 'replacement_style' for images. $this->assertSame('replacement_style', $formatter['settings']['image_style']); $this->assertSame('replacement_style', $widget['settings']['preview_image_style']); // Delete the 'replacement_style' without setting a replacement image style. $replacement->delete(); // The entity view and form displays exists after dependency removal. $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id())); $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id())); // The 'sticker' formatter component should be hidden in view display. $this->assertNull($view_display->getComponent('sticker')); $this->assertTrue($view_display->get('hidden')['sticker']); // The 'sticker' widget component should be active in form displays, but the // image preview should be disabled. $this->assertNotNull($widget = $form_display->getComponent('sticker')); $this->assertSame('', $widget['settings']['preview_image_style']); }
/** * [@inheritdoc} */ public function cloneField(ScheduledUpdateTypeInterface $scheduled_update_type, $field_name, $field_config_id = NULL) { $entity_type = $scheduled_update_type->getUpdateEntityType(); $definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type); if (!isset($definitions[$field_name])) { return FALSE; } $definition = $definitions[$field_name]; $new_field_name = $this->getNewFieldName($definition); $field_storage_values = ['field_name' => $new_field_name, 'entity_type' => 'scheduled_update', 'type' => $definition->getType(), 'translatable' => $definition->isTranslatable(), 'settings' => $definition->getSettings(), 'cardinality' => $definition->getCardinality()]; $field_values = ['field_name' => $new_field_name, 'entity_type' => 'scheduled_update', 'bundle' => $scheduled_update_type->id(), 'label' => $definition->getLabel(), 'translatable' => FALSE]; /** @var FieldConfig $field_config */ if ($field_config_id && ($field_config = FieldConfig::load($field_config_id))) { $field_values['settings'] = $field_config->getSettings(); $field_values['label'] = $field_config->label(); } // @todo Add Form display settings! FieldStorageConfig::create($field_storage_values)->save(); $field = FieldConfig::create($field_values); $field->save(); $destination_bundle = $scheduled_update_type->id(); /** @var EntityFormDisplay $destination_form_display */ $destination_form_display = EntityFormDisplay::load("scheduled_update.{$destination_bundle}.default"); if (empty($destination_form_display)) { $destination_form_display = EntityFormDisplay::create(['targetEntityType' => 'scheduled_update', 'bundle' => $destination_bundle, 'mode' => 'default', 'status' => TRUE]); } $display_options = []; if ($field_config_id) { $parts = explode('.', $field_config_id); $source_bundle = $parts[1]; /** @var EntityFormDisplay $source_form_display */ $source_form_display = EntityFormDisplay::load("{$entity_type}.{$source_bundle}.default"); $display_options = $source_form_display->getComponent($field_name); } else { if ($definition instanceof BaseFieldDefinition) { $display_options = $definition->getDisplayOptions('form'); if (empty($display_options)) { if ($definition->getType()) { // Provide default display for base boolean fields that don't have their own form display $display_options = ['type' => 'boolean_checkbox', 'settings' => ['display_label' => TRUE]]; } } } } if (empty($display_options)) { $display_options = []; } if ($destination_form_display) { $destination_form_display->setComponent($new_field_name, $display_options); $destination_form_display->save(); } else { // Alert user if display options could not be created. // @todo Create default display options even none on source. drupal_set_message($this->t('Form display options could not be created for @label they will have to be created manually.', ['@label' => $field_values['label']]), 'warning'); } return $field; }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->drupalCreateContentType(['type' => 'article']); $this->adminUser = $this->drupalCreateUser(['create article content', 'edit own article content', 'administer content types', 'administer node fields']); $this->drupalLogin($this->adminUser); // Add the address field to the article content type. $field_storage = FieldStorageConfig::create(['field_name' => 'field_address', 'entity_type' => 'node', 'type' => 'address']); $field_storage->save(); $this->field = FieldConfig::create(['field_storage' => $field_storage, 'bundle' => 'article', 'label' => 'Address']); $this->field->save(); // Set article's form display. $this->formDisplay = EntityFormDisplay::load('node.article.default'); if (!$this->formDisplay) { $this->formDisplay = EntityFormDisplay::create(['targetEntityType' => 'node', 'bundle' => 'article', 'mode' => 'default', 'status' => TRUE])->save(); } $this->formDisplay->setComponent($this->field->getName(), ['type' => 'address_default', 'settings' => ['default_country' => 'US']])->save(); $this->nodeAddUrl = 'node/add/article'; $this->fieldConfigUrl = 'admin/structure/types/manage/article/fields/node.article.' . $this->field->getName(); $this->countryRepository = \Drupal::service('address.country_repository'); $this->subdivisionRepository = \Drupal::service('address.subdivision_repository'); $this->addressFormatRepository = \Drupal::service('address.address_format_repository'); }
/** * Tests the form display of the label for multi-value fields. */ public function testLabelOnMultiValueFields() { $user = $this->drupalCreateUser(['administer entity_test content']); $this->drupalLogin($user); FieldStorageConfig::create(['entity_type' => 'entity_test_base_field_display', 'field_name' => 'foo', 'type' => 'text', 'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED])->save(); FieldConfig::create(['entity_type' => 'entity_test_base_field_display', 'bundle' => 'bar', 'field_name' => 'foo', 'label' => "<script>alert('a configurable field');</script>"])->save(); EntityFormDisplay::create(['targetEntityType' => 'entity_test_base_field_display', 'bundle' => 'bar', 'mode' => 'default'])->setComponent('foo', ['type' => 'text_textfield'])->enable()->save(); $entity = EntityTestBaseFieldDisplay::create(['type' => 'bar']); $entity->save(); $this->drupalGet('entity_test_base_field_display/manage/' . $entity->id()); $this->assertResponse(200); $this->assertText('A field with multiple values'); // Test if labels were XSS filtered. $this->assertEscaped("<script>alert('a configurable field');</script>"); }
/** * Tests \Drupal\Core\Entity\EntityDisplayBase::onDependencyRemoval(). */ public function testOnDependencyRemoval() { $this->enableModules(array('field_plugins_test')); $field_name = 'test_field'; // Create a field. $field_storage = FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'text')); $field_storage->save(); $field = FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'entity_test')); $field->save(); EntityFormDisplay::create(array('targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'default'))->setComponent($field_name, array('type' => 'field_plugins_test_text_widget'))->save(); // Check the component exists and is of the correct type. $display = entity_get_form_display('entity_test', 'entity_test', 'default'); $this->assertEqual($display->getComponent($field_name)['type'], 'field_plugins_test_text_widget'); // Removing the field_plugins_test module should change the component to use // the default widget for test fields. \Drupal::service('config.manager')->uninstall('module', 'field_plugins_test'); $display = entity_get_form_display('entity_test', 'entity_test', 'default'); $this->assertEqual($display->getComponent($field_name)['type'], 'text_textfield'); // Removing the text module should remove the field from the form display. \Drupal::service('config.manager')->uninstall('module', 'text'); $display = entity_get_form_display('entity_test', 'entity_test', 'default'); $this->assertFalse($display->getComponent($field_name)); }
/** * Tests that UUID isn't cached in form state on register form. * * This is a regression test for https://www.drupal.org/node/2500527 to ensure * that the form is not cached on GET requests. */ public function testUuidFormState() { \Drupal::service('module_installer')->install(['image']); \Drupal::service('router.builder')->rebuild(); // Add a picture field in order to ensure that no form cache is written, // which breaks registration of more than 1 user every 6 hours. $field_storage = FieldStorageConfig::create(['field_name' => 'user_picture', 'entity_type' => 'user', 'type' => 'image']); $field_storage->save(); $field = FieldConfig::create(['field_name' => 'user_picture', 'entity_type' => 'user', 'bundle' => 'user']); $field->save(); $form_display = EntityFormDisplay::create(['targetEntityType' => 'user', 'bundle' => 'user', 'mode' => 'default', 'status' => TRUE]); $form_display->setComponent('user_picture', ['type' => 'image_image']); $form_display->save(); // Don't require email verification and allow registration by site visitors // without administrator approval. $this->config('user.settings')->set('verify_mail', FALSE)->set('register', USER_REGISTER_VISITORS)->save(); $edit = []; $edit['name'] = $this->randomMachineName(); $edit['mail'] = $edit['name'] . '@example.com'; $edit['pass[pass2]'] = $edit['pass[pass1]'] = $this->randomMachineName(); // Create one account. $this->drupalPostForm('user/register', $edit, t('Create new account')); $this->assertResponse(200); $user_storage = \Drupal::entityManager()->getStorage('user'); $this->assertTrue($user_storage->loadByProperties(['name' => $edit['name']])); $this->drupalLogout(); // Create a second account. $edit['name'] = $this->randomMachineName(); $edit['mail'] = $edit['name'] . '@example.com'; $edit['pass[pass2]'] = $edit['pass[pass1]'] = $this->randomMachineName(); $this->drupalPostForm('user/register', $edit, t('Create new account')); $this->assertResponse(200); $this->assertTrue($user_storage->loadByProperties(['name' => $edit['name']])); }
/** * Tests components dependencies additions. */ public function testComponentDependencies() { $this->enableModules(['dblog', 'color']); $this->installSchema('dblog', ['watchdog']); $this->installEntitySchema('user'); /** @var \Drupal\user\RoleInterface[] $roles */ $roles = []; // Create two arbitrary user roles. for ($i = 0; $i < 2; $i++) { $roles[$i] = Role::create(['id' => Unicode::strtolower($this->randomMachineName()), 'label' => $this->randomString()]); $roles[$i]->save(); } // Create a field of type 'test_field' attached to 'entity_test'. $field_name = Unicode::strtolower($this->randomMachineName()); FieldStorageConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'test_field'])->save(); FieldConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'bundle' => 'entity_test'])->save(); // Create a new form display without components. /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ $form_display = EntityFormDisplay::create(['targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'default']); $form_display->save(); $dependencies = ['user.role.' . $roles[0]->id(), 'user.role.' . $roles[1]->id()]; // The config object should not depend on none of the two $roles. $this->assertNoDependency('config', $dependencies[0], $form_display); $this->assertNoDependency('config', $dependencies[1], $form_display); // Add a widget of type 'test_field_widget'. $component = ['type' => 'test_field_widget', 'settings' => ['test_widget_setting' => $this->randomString(), 'role' => $roles[0]->id(), 'role2' => $roles[1]->id()], 'third_party_settings' => ['color' => ['foo' => 'bar']]]; $form_display->setComponent($field_name, $component); $form_display->save(); // Now, the form display should depend on both user roles $roles. $this->assertDependency('config', $dependencies[0], $form_display); $this->assertDependency('config', $dependencies[1], $form_display); // The form display should depend on 'color' module. $this->assertDependency('module', 'color', $form_display); // Delete the first user role entity. $roles[0]->delete(); // Reload the form display. $form_display = EntityFormDisplay::load($form_display->id()); // The display exists. $this->assertFalse(empty($form_display)); // The form display should not depend on $role[0] anymore. $this->assertNoDependency('config', $dependencies[0], $form_display); // The form display should depend on 'anonymous' user role. $this->assertDependency('config', 'user.role.anonymous', $form_display); // The form display should depend on 'color' module. $this->assertDependency('module', 'color', $form_display); // Manually trigger the removal of configuration belonging to the module // because KernelTestBase::disableModules() is not aware of this. $this->container->get('config.manager')->uninstall('module', 'color'); // Uninstall 'color' module. $this->disableModules(['color']); // Reload the form display. $form_display = EntityFormDisplay::load($form_display->id()); // The display exists. $this->assertFalse(empty($form_display)); // The component is still enabled. $this->assertNotNull($form_display->getComponent($field_name)); // The form display should not depend on 'color' module anymore. $this->assertNoDependency('module', 'color', $form_display); // Delete the 2nd user role entity. $roles[1]->delete(); // Reload the form display. $form_display = EntityFormDisplay::load($form_display->id()); // The display exists. $this->assertFalse(empty($form_display)); // The component has been disabled. $this->assertNull($form_display->getComponent($field_name)); $this->assertTrue($form_display->get('hidden')[$field_name]); // The correct warning message has been logged. $arguments = ['@display' => (string) t('Entity form display'), '@id' => $form_display->id(), '@name' => $field_name]; $logged = (bool) Database::getConnection()->select('watchdog', 'w')->fields('w', ['wid'])->condition('type', 'system')->condition('message', "@display '@id': Component '@name' was disabled because its settings depend on removed dependencies.")->condition('variables', serialize($arguments))->execute()->fetchAll(); $this->assertTrue($logged); }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { /** @var \Drupal\contact\ContactFormInterface $contact_form */ $contact_form = $this->entity; // Get the original ID. $original_id = $contact_form->getOriginalId(); $new_id = $contact_form->id(); // Create the new form. $contact_form = $contact_form->createDuplicate(); $contact_form->set('id', $new_id); $contact_form->save(); // Clone configurable fields. foreach ($this->fieldManager->getFieldDefinitions('contact_message', $original_id) as $field) { if ($field instanceof BaseFieldDefinition) { continue; } if ($this->moduleHandler->moduleExists('field')) { if ($config = $field->getConfig($original_id)) { $new_config = FieldConfig::create([ 'bundle' => $contact_form->id(), 'uuid' => NULL, ] + $config->toArray()); $new_config->save(); } } } // Clone the entity form display. $display = EntityFormDisplay::load('contact_message.' . $original_id . '.default'); EntityFormDisplay::create([ 'bundle' => $contact_form->id(), 'uuid' => NULL, ] + $display->toArray())->save(); // Clone the entity view display. $display = EntityViewDisplay::load('contact_message.' . $original_id . '.default'); EntityViewDisplay::create([ 'bundle' => $contact_form->id(), 'uuid' => NULL, ] + $display->toArray())->save(); // Redirect and show messge. $form_state->setRedirect('entity.contact_form.edit_form', ['contact_form' => $contact_form->id()]); $edit_link = $this->entity->link($this->t('Edit')); drupal_set_message($this->t('Contact form %label has been added.', array('%label' => $contact_form->label()))); $this->logger('contact')->notice('Contact form %label has been added.', array('%label' => $contact_form->label(), 'link' => $edit_link)); }
/** * Adds a Paragraphs field to a given $entity_type. * * @param string $entity_type_name * Entity type name to be used. * @param string $paragraphs_field_name * Paragraphs field name to be used. * @param string $entity_type * Entity type where to add the field. */ protected function addParagraphsField($entity_type_name, $paragraphs_field_name, $entity_type) { // Add a paragraphs field. $field_storage = FieldStorageConfig::create(['field_name' => $paragraphs_field_name, 'entity_type' => $entity_type, 'type' => 'entity_reference_revisions', 'settings' => ['target_type' => 'paragraph', 'cardinality' => '-1']]); $field_storage->save(); $field = FieldConfig::create(['field_storage' => $field_storage, 'bundle' => $entity_type_name, 'settings' => ['handler' => 'default:paragraph', 'handler_settings' => ['target_bundles' => NULL]]]); $field->save(); $form_display = EntityFormDisplay::create(['targetEntityType' => $entity_type, 'bundle' => $entity_type_name, 'mode' => 'default', 'status' => TRUE])->setComponent($paragraphs_field_name, ['type' => 'entity_reference_paragraphs']); $form_display->save(); $view_display = EntityViewDisplay::create(['targetEntityType' => $entity_type, 'bundle' => $entity_type_name, 'mode' => 'default', 'status' => TRUE])->setComponent($paragraphs_field_name, ['type' => 'entity_reference_revisions_entity_view']); $view_display->save(); }
/** * Test getDisplayModeOptions(). */ public function testGetDisplayModeOptions() { NodeType::create(array('type' => 'article'))->save(); EntityViewDisplay::create(array('targetEntityType' => 'node', 'bundle' => 'article', 'mode' => 'default'))->setStatus(TRUE)->save(); $display_teaser = EntityViewDisplay::create(array('targetEntityType' => 'node', 'bundle' => 'article', 'mode' => 'teaser')); $display_teaser->save(); EntityFormDisplay::create(array('targetEntityType' => 'user', 'bundle' => 'user', 'mode' => 'default'))->setStatus(TRUE)->save(); $form_display_teaser = EntityFormDisplay::create(array('targetEntityType' => 'user', 'bundle' => 'user', 'mode' => 'register')); $form_display_teaser->save(); // Test getViewModeOptionsByBundle(). $view_modes = \Drupal::entityManager()->getViewModeOptionsByBundle('node', 'article'); $this->assertEqual($view_modes, array('default' => 'Default')); $display_teaser->setStatus(TRUE)->save(); $view_modes = \Drupal::entityManager()->getViewModeOptionsByBundle('node', 'article'); $this->assertEqual($view_modes, array('default' => 'Default', 'teaser' => 'Teaser')); // Test getFormModeOptionsByBundle(). $form_modes = \Drupal::entityManager()->getFormModeOptionsByBundle('user', 'user'); $this->assertEqual($form_modes, array('default' => 'Default')); $form_display_teaser->setStatus(TRUE)->save(); $form_modes = \Drupal::entityManager()->getFormModeOptionsByBundle('user', 'user'); $this->assertEqual($form_modes, array('default' => 'Default', 'register' => 'Register')); }
/** * Create a form display for a newly clone field. * * This function attempts to use same setting settings as the source field. * * @param \Drupal\scheduled_updates\ScheduledUpdateTypeInterface $scheduled_update_type * @param $field_name * @param $field_config_id * @param $entity_type * @param $definition * Source field definition * @param $new_field_name */ protected function createFormDisplay(ScheduledUpdateTypeInterface $scheduled_update_type, $field_config_id, FieldStorageDefinitionInterface $definition, $new_field_name) { $destination_bundle = $scheduled_update_type->id(); $field_name = $definition->getName(); $entity_type = $scheduled_update_type->getUpdateEntityType(); /** @var EntityFormDisplay $destination_form_display */ $destination_form_display = EntityFormDisplay::load("scheduled_update.$destination_bundle.default"); if (empty($destination_form_display)) { $destination_form_display = EntityFormDisplay::create([ 'targetEntityType' => 'scheduled_update', 'bundle' => $destination_bundle, 'mode' => 'default', 'status' => TRUE, ]); } $display_options = []; if ($field_config_id) { $parts = explode('.', $field_config_id); $source_bundle = $parts[1]; /** @var EntityFormDisplay $source_form_display */ $source_form_display = EntityFormDisplay::load("$entity_type.$source_bundle.default"); $display_options = $source_form_display->getComponent($field_name); } else { if ($definition instanceof BaseFieldDefinition) { $display_options = $definition->getDisplayOptions('form'); if (empty($display_options)) { if ($definition->getType()) { // Provide default display for base boolean fields that don't have their own form display $display_options = [ 'type' => 'boolean_checkbox', 'settings' => ['display_label' => TRUE], ]; } } } } if (empty($display_options)) { $display_options = []; } if ($destination_form_display) { $destination_form_display->setComponent($new_field_name, $display_options); $destination_form_display->save(); } else { // Alert user if display options could not be created. // @todo Create default display options even none on source. drupal_set_message( $this->t( 'Form display options could not be created for @field they will have to be created manually.', ['@field' => $field_name] ), 'warning'); } }