/** * Returns matched labels based on a given field, instance and search string. * * This function can be used by other modules that wish to pass a mocked * definition of the field on instance. * * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition * The field definition. * @param string $entity_type * The entity type. * @param string $bundle * The entity bundle. * @param string $entity_id * (optional) The entity ID the entity reference field is attached to. * Defaults to ''. * @param string $prefix * (optional) A prefix for all the keys returned by this function. * @param string $string * (optional) The label of the entity to query by. * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException * Thrown when the current user doesn't have access to the specifies entity. * * @return array * A list of matched entity labels. * * @see \Drupal\entity_reference\EntityReferenceController */ public function getMatches(FieldDefinitionInterface $field_definition, $entity_type, $bundle, $entity_id = '', $prefix = '', $string = '') { $matches = array(); $entity = NULL; if ($entity_id !== 'NULL') { $entity = $this->entityManager->getStorage($entity_type)->load($entity_id); if (!$entity || !$entity->access('view')) { throw new AccessDeniedHttpException(); } } $handler = $this->selectionHandlerManager->getSelectionHandler($field_definition, $entity); if (isset($string)) { // Get an array of matching entities. $widget = entity_get_form_display($entity_type, $bundle, 'default')->getComponent($field_definition->getName()); $match_operator = !empty($widget['settings']['match_operator']) ? $widget['settings']['match_operator'] : 'CONTAINS'; $entity_labels = $handler->getReferenceableEntities($string, $match_operator, 10); // Loop through the entities and convert them into autocomplete output. foreach ($entity_labels as $values) { foreach ($values as $entity_id => $label) { $key = "{$label} ({$entity_id})"; // Strip things like starting/trailing white spaces, line breaks and // tags. $key = preg_replace('/\\s\\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key))))); // Names containing commas or quotes must be wrapped in quotes. $key = Tags::encode($key); $matches[] = array('value' => $prefix . $key, 'label' => $label); } } } return $matches; }
/** * Tests boolean field. */ function testBooleanField() { $on = $this->randomMachineName(); $off = $this->randomMachineName(); $label = $this->randomMachineName(); // Create a field with settings to validate. $field_name = drupal_strtolower($this->randomMachineName()); $this->field_storage = FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'boolean', 'settings' => array('on_label' => $on, 'off_label' => $off))); $this->field_storage->save(); $this->field = FieldConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'bundle' => 'entity_test', 'label' => $label, 'required' => TRUE)); $this->field->save(); // Create a form display for the default form mode. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, array('type' => 'boolean_checkbox'))->save(); // Create a display for the full view mode. entity_get_display('entity_test', 'entity_test', 'full')->setComponent($field_name, array('type' => 'boolean'))->save(); // Display creation form. $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.'); $this->assertRaw($on); // Submit and ensure it is accepted. $edit = array("{$field_name}[value]" => 1); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\\d+)|', $this->url, $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); // Verify that boolean value is displayed. $entity = entity_load('entity_test', $id); $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); $content = $display->build($entity); $this->drupalSetContent(drupal_render($content)); $this->assertRaw('<div class="field-item">' . $on . '</div>'); // Test the display_label option. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, array('type' => 'boolean_checkbox', 'settings' => array('display_label' => TRUE)))->save(); $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.'); $this->assertNoRaw($on); $this->assertText($this->field->label()); // Go to the form display page and check if the default settings works as // expected. $fieldEditUrl = 'entity_test/structure/entity_test/form-display'; $this->drupalGet($fieldEditUrl); // Click on the widget settings button to open the widget settings form. $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit"); $this->assertText('Use field label instead of the "On label" as label', t('Display setting checkbox available.')); // Enable setting. $edit = array('fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1); $this->drupalPostAjaxForm(NULL, $edit, $field_name . "_plugin_settings_update"); $this->drupalPostForm(NULL, NULL, 'Save'); // Go again to the form display page and check if the setting // is stored and has the expected effect. $this->drupalGet($fieldEditUrl); $this->assertText('Use field label: Yes', 'Checking the display settings checkbox updated the value.'); $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit"); $this->assertText('Use field label instead of the "On label" as label', t('Display setting checkbox is available')); $this->assertFieldByXPath('*//input[@id="edit-fields-' . $field_name . '-settings-edit-form-settings-display-label" and @value="1"]', TRUE, t('Display label changes label of the checkbox')); // Test the boolean field settings. $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name . '/storage'); $this->assertFieldById('edit-field-storage-settings-on-label', $on); $this->assertFieldById('edit-field-storage-settings-off-label', $off); }
protected function setUp() { parent::setUp(); // Set up an additional language. $this->langcodes = array(language_default()->getId(), 'es'); ConfigurableLanguage::createFromLangcode('es')->save(); // Create a content type. $this->bundle = $this->randomMachineName(); $this->contentType = $this->drupalCreateContentType(array('type' => $this->bundle)); // Enable translation for the current entity type and ensure the change is // picked up. content_translation_set_config('node', $this->bundle, 'enabled', TRUE); drupal_static_reset(); \Drupal::entityManager()->clearCachedBundles(); \Drupal::service('router.builder')->rebuild(); // Add a translatable field to the content type. entity_create('field_storage_config', array('field_name' => 'field_test_text', 'entity_type' => 'node', 'type' => 'text', 'cardinality' => 1, 'translatable' => TRUE))->save(); entity_create('field_config', array('entity_type' => 'node', 'field_name' => 'field_test_text', 'bundle' => $this->bundle, 'label' => 'Test text-field'))->save(); entity_get_form_display('node', $this->bundle, 'default')->setComponent('field_test_text', array('type' => 'text_textfield', 'weight' => 0))->save(); // Enable content translation. $configuration = array('langcode' => language_default()->getId(), 'language_show' => TRUE); language_save_default_configuration('node', $this->bundle, $configuration); // Create a translator user. $permissions = array('access contextual links', 'administer nodes', "edit any {$this->bundle} content", 'translate any entity'); $this->translator = $this->drupalCreateUser($permissions); }
/** * Checks block edit functionality. */ public function testBlockFields() { $this->drupalLogin($this->adminUser); $this->blockType = $this->createBlockContentType('link'); // Create a field with settings to validate. $this->fieldStorage = entity_create('field_storage_config', array('name' => drupal_strtolower($this->randomName()), 'entity_type' => 'block_content', 'type' => 'link', 'cardinality' => 2)); $this->fieldStorage->save(); $this->instance = entity_create('field_instance_config', array('field_storage' => $this->fieldStorage, 'bundle' => 'link', 'settings' => array('title' => DRUPAL_OPTIONAL))); $this->instance->save(); entity_get_form_display('block_content', 'link', 'default')->setComponent($this->fieldStorage->getName(), array('type' => 'link_default'))->save(); entity_get_display('block_content', 'link', 'default')->setComponent($this->fieldStorage->getName(), array('type' => 'link', 'label' => 'hidden'))->save(); // Create a block. $this->drupalGet('block/add/link'); $edit = array('info[0][value]' => $this->randomName(8), $this->fieldStorage->getName() . '[0][url]' => 'http://example.com', $this->fieldStorage->getName() . '[0][title]' => 'Example.com'); $this->drupalPostForm(NULL, $edit, t('Save')); $block = entity_load('block_content', 1); $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . \Drupal::config('system.theme')->get('default'); // Place the block. $instance = array('id' => drupal_strtolower($edit['info[0][value]']), 'settings[label]' => $edit['info[0][value]'], 'region' => 'sidebar_first'); $this->drupalPostForm($url, $instance, t('Save block')); // Navigate to home page. $this->drupalGet('<front>'); $this->assertLinkByHref('http://example.com'); $this->assertText('Example.com'); }
/** * Test ID validation and the proper video display of a valid ID. */ public function testVideo() { $field_name = Unicode::strtolower($this->randomMachineName()); // Create a field. $field_storage = entity_create('field_storage_config', array('field_name' => $field_name, 'entity_type' => 'node', 'translatable' => FALSE, 'type' => 'youtube', 'cardinality' => '1')); $field_storage->save(); $field = entity_create('field_config', array('field_storage' => $field_storage, 'bundle' => 'article', 'title' => DRUPAL_DISABLED)); $field->save(); entity_get_form_display('node', 'article', 'default')->setComponent($field_name, array('type' => 'youtube', 'settings' => array()))->save(); entity_get_display('node', 'article', 'full')->setComponent($field_name, array('type' => 'youtube_video'))->save(); // Display creation form. $this->drupalGet('node/add/article'); $this->assertFieldByName("{$field_name}[0][input]", '', t('Video input field is displayed')); // Verify that a valid URL can be submitted. $video_id = 'T5y3dJYHb_A'; $value = 'http://www.youtube.com/watch?v=' . $video_id; $embed_value = 'http://www.youtube.com/embed/' . $video_id; $edit = array("title[0][value]" => 'Test', "{$field_name}[0][input]" => $value); $this->drupalPostForm(NULL, $edit, t('Save and publish')); preg_match('|/node/(\\d+)|', $this->url, $match); $this->assertText(t('Article Test has been created.')); $this->assertRaw($embed_value); // Verify that the video is displayed. $pattern = '<iframe.*src="' . $embed_value; $pattern = str_replace('/', '\\/', $pattern); $pattern = '/' . $pattern . '/s'; $this->assertPattern($pattern); // Verify that invalid URLs cannot be submitted. $this->drupalGet('node/add/article'); $value = 'not-a-url'; $edit = array("title[0][value]" => 'Test1', "{$field_name}[0][input]" => $value); $this->drupalPostForm(NULL, $edit, t('Save and publish')); $this->assertText(t('Please provide a valid YouTube URL.')); }
/** * Helper function for testTextfieldWidgets(). */ function _testTextfieldWidgets($field_type, $widget_type) { // Create a field. $field_name = Unicode::strtolower($this->randomMachineName()); $field_storage = FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => $field_type)); $field_storage->save(); FieldConfig::create(['field_storage' => $field_storage, 'bundle' => 'entity_test', 'label' => $this->randomMachineName() . '_label'])->save(); entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, array('type' => $widget_type, 'settings' => array('placeholder' => 'A placeholder on ' . $widget_type)))->save(); entity_get_display('entity_test', 'entity_test', 'full')->setComponent($field_name)->save(); // Display creation form. $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed'); $this->assertNoFieldByName("{$field_name}[0][format]", '1', 'Format selector is not displayed'); $this->assertRaw(format_string('placeholder="A placeholder on @widget_type"', array('@widget_type' => $widget_type))); // Submit with some value. $value = $this->randomMachineName(); $edit = array("{$field_name}[0][value]" => $value); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\\d+)|', $this->url, $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', array('@id' => $id)), 'Entity was created'); // Display the entity. $entity = EntityTest::load($id); $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); $content = $display->build($entity); $this->setRawContent(\Drupal::service('renderer')->renderRoot($content)); $this->assertText($value, 'Filtered tags are not displayed'); }
/** * Test the behavior of a field module after being disabled and re-enabled. */ function testReEnabledField() { // Add a telephone field to the article content type. $field_storage = entity_create('field_storage_config', array('name' => 'field_telephone', 'entity_type' => 'node', 'type' => 'telephone')); $field_storage->save(); entity_create('field_instance_config', array('field_storage' => $field_storage, 'bundle' => 'article', 'label' => 'Telephone Number'))->save(); entity_get_form_display('node', 'article', 'default')->setComponent('field_telephone', array('type' => 'telephone_default', 'settings' => array('placeholder' => '123-456-7890')))->save(); entity_get_display('node', 'article', 'default')->setComponent('field_telephone', array('type' => 'telephone_link', 'weight' => 1))->save(); // Display the article node form and verify the telephone widget is present. $this->drupalGet('node/add/article'); $this->assertFieldByName("field_telephone[0][value]", '', 'Widget found.'); // Submit an article node with a telephone field so data exist for the // field. $edit = array('title[0][value]' => $this->randomName(), 'field_telephone[0][value]' => "123456789"); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertRaw('<a href="tel:123456789">'); // Test that the module can't be uninstalled from the UI while there is data // for it's fields. $admin_user = $this->drupalCreateUser(array('access administration pages', 'administer modules')); $this->drupalLogin($admin_user); $this->drupalGet('admin/modules'); $this->assertText('Fields type(s) in use'); $field_storage->delete(); $this->drupalGet('admin/modules'); $this->assertText('Fields pending deletion'); $this->cronRun(); $this->assertNoText('Fields type(s) in use'); $this->assertNoText('Fields pending deletion'); }
protected function setUp() { parent::setUp(); // Let there be T-rex. \Drupal::state()->set('editor_test_give_me_a_trex_thanks', TRUE); \Drupal::service('plugin.manager.editor')->clearCachedDefinitions(); // Add text formats. $filtered_html_format = entity_create('filter_format', array('format' => 'filtered_html', 'name' => 'Filtered HTML', 'weight' => 0, 'filters' => array())); $filtered_html_format->save(); $full_html_format = entity_create('filter_format', array('format' => 'full_html', 'name' => 'Full HTML', 'weight' => 1, 'filters' => array())); $full_html_format->save(); // Create article node type. $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); // Create page node type, but remove the body. $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Page')); $body = FieldConfig::loadByName('node', 'page', 'body'); $body->delete(); // Create a formatted text field, which uses an <input type="text">. FieldStorageConfig::create(array('field_name' => 'field_text', 'entity_type' => 'node', 'type' => 'text'))->save(); FieldConfig::create(array('field_name' => 'field_text', 'entity_type' => 'node', 'label' => 'Textfield', 'bundle' => 'page'))->save(); entity_get_form_display('node', 'page', 'default')->setComponent('field_text')->save(); // Create 3 users, each with access to different text formats. $this->untrustedUser = $this->drupalCreateUser(array('create article content', 'edit any article content')); $this->normalUser = $this->drupalCreateUser(array('create article content', 'edit any article content', 'use text format filtered_html')); $this->privilegedUser = $this->drupalCreateUser(array('create article content', 'edit any article content', 'create page content', 'edit any page content', 'use text format filtered_html', 'use text format full_html')); }
/** * Tests the field's entity form display settings. */ public function testEntityFormDisplaySettings() { $component = entity_get_form_display('user', 'user', 'default')->getComponent('user_picture'); $this->assertIdentical('image_image', $component['type']); $this->assertIdentical('throbber', $component['settings']['progress_indicator']); $this->assertIdentical('thumbnail', $component['settings']['preview_image_style']); }
/** * Tests email field. */ function testEmailField() { // Create a field with settings to validate. $field_name = Unicode::strtolower($this->randomMachineName()); $this->fieldStorage = FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'email')); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_storage' => $this->fieldStorage, 'bundle' => 'entity_test']); $this->field->save(); // Create a form display for the default form mode. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($field_name, array('type' => 'email_default', 'settings' => array('placeholder' => '*****@*****.**')))->save(); // Create a display for the full view mode. entity_get_display('entity_test', 'entity_test', 'full')->setComponent($field_name, array('type' => 'email_mailto'))->save(); // Display creation form. $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget found.'); $this->assertRaw('placeholder="*****@*****.**"'); // Submit a valid email address and ensure it is accepted. $value = '*****@*****.**'; $edit = array("{$field_name}[0][value]" => $value); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\\d+)|', $this->url, $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); $this->assertRaw($value); // Verify that a mailto link is displayed. $entity = EntityTest::load($id); $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); $content = $display->build($entity); $this->setRawContent(\Drupal::service('renderer')->renderRoot($content)); $this->assertLinkByHref('mailto:test@example.com'); }
/** * Setup a new content type, with a image/file field. */ protected function initNode() { // Create a new content type. $this->drupalCreateContentType(array('type' => $this->instBundle, 'name' => $this->instBundle)); // Prep a field base. $field_storage_settings = array('display_field' => TRUE, 'display_default' => TRUE, 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); $field_storage = array('entity_type' => 'node', 'field_name' => $this->instFieldName, 'type' => $this->instFieldType, 'settings' => $field_storage_settings, 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); entity_create('field_storage_config', $field_storage)->save(); // Prep a field instance. $field_settings = array(); if ($this->instFieldType == 'image') { $field_settings['alt_field'] = TRUE; $field_settings['alt_field_required'] = FALSE; $field_settings['title_field'] = TRUE; $field_settings['title_field_required'] = FALSE; } if ($this->instFieldType == 'file') { $field_settings['description_field'] = TRUE; $field_settings['file_extensions'] = 'txt jpg png mp3 rtf docx pdf'; } $field = array('field_name' => $this->instFieldName, 'label' => $this->randomString(), 'entity_type' => 'node', 'bundle' => $this->instBundle, 'required' => FALSE, 'settings' => $field_settings); entity_create('field_config', $field)->save(); // Setup widget. entity_get_form_display('node', $this->instBundle, 'default')->setComponent($this->instFieldName, array('type' => 'file_generic', 'settings' => array()))->save(); // Clear some caches for good measure. $entity_manager = $this->container->get('entity.manager'); $entity_manager->getStorage('field_storage_config')->resetCache(); $entity_manager->getStorage('field_config')->resetCache(); }
/** * Tests that a form's action is retained after an Ajax submission. * * The 'action' attribute of a form should not change after an Ajax submission * followed by a non-Ajax submission, which triggers a validation error. */ function testPreserveFormActionAfterAJAX() { // Create a multi-valued field for 'page' nodes to use for Ajax testing. $field_name = 'field_ajax_test'; FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => 'node', 'type' => 'text', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED))->save(); FieldConfig::create(['field_name' => $field_name, 'entity_type' => 'node', 'bundle' => 'page'])->save(); entity_get_form_display('node', 'page', 'default')->setComponent($field_name, array('type' => 'text_textfield'))->save(); // Log in a user who can create 'page' nodes. $this->webUser = $this->drupalCreateUser(array('create page content')); $this->drupalLogin($this->webUser); // Get the form for adding a 'page' node. Submit an "add another item" Ajax // submission and verify it worked by ensuring the updated page has two text // field items in the field for which we just added an item. $this->drupalGet('node/add/page'); $this->drupalPostAjaxForm(NULL, array(), array('field_ajax_test_add_more' => t('Add another item')), NULL, array(), array(), 'node-page-form'); $this->assert(count($this->xpath('//div[contains(@class, "field--name-field-ajax-test")]//input[@type="text"]')) == 2, 'AJAX submission succeeded.'); // Submit the form with the non-Ajax "Save" button, leaving the title field // blank to trigger a validation error, and ensure that a validation error // occurred, because this test is for testing what happens when a form is // re-rendered without being re-built, which is what happens when there's // a validation error. $this->drupalPostForm(NULL, array(), t('Save')); $this->assertText('Title field is required.', 'Non-AJAX submission correctly triggered a validation error.'); // Ensure that the form contains two items in the multi-valued field, so we // know we're testing a form that was correctly retrieved from cache. $this->assert(count($this->xpath('//form[contains(@id, "node-page-form")]//div[contains(@class, "js-form-item-field-ajax-test")]//input[@type="text"]')) == 2, 'Form retained its state from cache.'); // Ensure that the form's action is correct. $forms = $this->xpath('//form[contains(@class, "node-page-form")]'); $this->assertEqual(1, count($forms)); // Strip query params off the action before asserting. $url = parse_url($forms[0]['action'])['path']; $this->assertEqual(Url::fromRoute('node.add', ['node_type' => 'page'])->toString(), $url); }
protected function setUp() { parent::setUp(); $this->addDefaultCommentField('node', 'page'); $web_user = $this->drupalCreateUser(array('edit own page content', 'create page content')); $this->drupalLogin($web_user); // Add a vocabulary so we can test different view modes. $vocabulary = entity_create('taxonomy_vocabulary', array('name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'vid' => $this->randomMachineName(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'help' => '')); $vocabulary->save(); $this->vocabulary = $vocabulary; // Add a term to the vocabulary. $term = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'vid' => $this->vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $term->save(); $this->term = $term; // Create an image field. FieldStorageConfig::create(['field_name' => 'field_image', 'entity_type' => 'node', 'type' => 'image', 'settings' => [], 'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED])->save(); $field_config = FieldConfig::create(['field_name' => 'field_image', 'label' => 'Images', 'entity_type' => 'node', 'bundle' => 'page', 'required' => FALSE, 'settings' => []]); $field_config->save(); // Create a field. $this->fieldName = Unicode::strtolower($this->randomMachineName()); $handler_settings = array('target_bundles' => array($this->vocabulary->id() => $this->vocabulary->id()), 'auto_create' => TRUE); $this->createEntityReferenceField('node', 'page', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); entity_get_form_display('node', 'page', 'default')->setComponent($this->fieldName, array('type' => 'entity_reference_autocomplete_tags'))->save(); // Show on default display and teaser. entity_get_display('node', 'page', 'default')->setComponent($this->fieldName, array('type' => 'entity_reference_label'))->save(); entity_get_display('node', 'page', 'teaser')->setComponent($this->fieldName, array('type' => 'entity_reference_label'))->save(); entity_get_form_display('node', 'page', 'default')->setComponent('field_image', array('type' => 'image_image', 'settings' => []))->save(); entity_get_display('node', 'page', 'default')->setComponent('field_image')->save(); }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); if ($this->profile != 'standard') { $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page', 'settings' => array('node' => array('options' => array('promote' => FALSE), 'submitted' => FALSE)))); $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); } $this->admin_user = $this->drupalCreateUser(array('administer nodes', 'bypass node access', 'administer content types', 'administer xmlsitemap', 'administer taxonomy')); $this->normal_user = $this->drupalCreateUser(array('create page content', 'edit any page content', 'access content', 'view own unpublished content')); // allow anonymous user to view user profiles $user_role = entity_load('user_role', DRUPAL_ANONYMOUS_RID); $user_role->grantPermission('access content'); $user_role->save(); xmlsitemap_link_bundle_enable('node', 'article'); xmlsitemap_link_bundle_enable('node', 'page'); $this->config->set('xmlsitemap_entity_taxonomy_vocabulary', 1); $this->config->set('xmlsitemap_entity_taxonomy_term', 1); $this->config->save(); xmlsitemap_link_bundle_settings_save('node', 'page', array('status' => 1, 'priority' => 0.6, 'changefreq' => XMLSITEMAP_FREQUENCY_WEEKLY)); // Add a vocabulary so we can test different view modes. $vocabulary = entity_create('taxonomy_vocabulary', array('name' => 'Tags', 'description' => $this->randomMachineName(), 'vid' => 'tags', 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'help' => '')); $vocabulary->save(); xmlsitemap_link_bundle_enable('taxonomy_term', 'tags'); // Set up a field and instance. $field_name = 'tags'; entity_create('field_storage_config', array('name' => $field_name, 'entity_type' => 'node', 'type' => 'taxonomy_term_reference', 'settings' => array('allowed_values' => array(array('vocabulary' => $vocabulary->vid, 'parent' => '0'))), 'cardinality' => '-1'))->save(); entity_create('field_instance_config', array('field_name' => $field_name, 'entity_type' => 'node', 'bundle' => 'page'))->save(); entity_get_form_display('node', 'page', 'default')->setComponent($field_name, array('type' => 'taxonomy_autocomplete'))->save(); // Show on default display and teaser. entity_get_display('node', 'page', 'default')->setComponent($field_name, array('type' => 'taxonomy_term_reference_link'))->save(); entity_get_display('node', 'page', 'teaser')->setComponent($field_name, array('type' => 'taxonomy_term_reference_link'))->save(); }
/** * Tests comment subject variable migrated into an entity display. */ public function testCommentEntityFormDisplay() { $component = entity_get_form_display('comment', 'comment', 'default')->getComponent('subject'); $this->assertIdentical('string_textfield', $component['type']); $this->assertIdentical(10, $component['weight']); $component = entity_get_form_display('comment', 'comment_no_subject', 'default')->getComponent('subject'); $this->assertNull($component); }
/** * Tests the Drupal 6 user picture to Drupal 8 entity form display migration. */ public function testUserPictureEntityFormDisplay() { $display = entity_get_form_display('user', 'user', 'default'); $component = $display->getComponent('user_picture'); $this->assertEqual($component['type'], 'image_image'); $this->assertEqual($component['settings']['progress_indicator'], 'throbber'); $this->assertEqual(array('user', 'user', 'default', 'user_picture'), entity_load('migration', 'd6_user_picture_entity_form_display')->getIdMap()->lookupDestinationID(array(''))); }
/** * Tests comment subject variable migrated into an entity display. */ public function testCommentEntityFormDisplay() { $component = entity_get_form_display('comment', 'comment', 'default')->getComponent('subject'); $this->assertEqual($component['type'], 'string'); $this->assertEqual($component['weight'], 10); $component = entity_get_form_display('comment', 'comment_no_subject', 'default')->getComponent('subject'); $this->assertNull($component); }
protected function setUp() { parent::setUp(); // Auto-create a field for testing. entity_create('field_storage_config', array('entity_type' => 'user', 'field_name' => 'test_multiple', 'type' => 'text', 'cardinality' => -1, 'translatable' => FALSE))->save(); entity_create('field_config', array('entity_type' => 'user', 'field_name' => 'test_multiple', 'bundle' => 'user', 'label' => 'Test a multiple valued field'))->save(); entity_get_form_display('user', 'user', 'register')->setComponent('test_multiple', array('type' => 'text_textfield', 'weight' => 0))->save(); }
/** * Adds term reference field for the article content type. */ protected function setUpTermReferenceField() { entity_create('field_storage_config', array('field_name' => $this->termFieldName, 'entity_type' => 'node', 'type' => 'taxonomy_term_reference', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'settings' => array('allowed_values' => array(array('vocabulary' => $this->vocabulary->id(), 'parent' => 0)))))->save(); $field = entity_create('field_config', array('field_name' => $this->termFieldName, 'bundle' => 'article', 'entity_type' => 'node', 'translatable' => FALSE)); $field->save(); entity_get_form_display('node', 'article', 'default')->setComponent($this->termFieldName, array('type' => 'taxonomy_autocomplete'))->save(); entity_get_display('node', 'article', 'default')->setComponent($this->termFieldName, array('type' => 'taxonomy_term_reference_link'))->save(); }
/** * Tests comment variables migrated into an entity display. */ public function testCommentEntityFormDisplay() { foreach ($this->types as $type) { $component = entity_get_form_display('node', $type, 'default')->getComponent('comment'); $this->assertIdentical('comment_default', $component['type']); $this->assertIdentical(20, $component['weight']); } }
/** * Test that allowed values can be updated. */ function testUpdateAllowedValues() { // All three options appear. $entity = entity_create('entity_test'); $form = \Drupal::service('entity.form_builder')->getForm($entity); $this->assertTrue(!empty($form[$this->fieldName]['widget'][1]), 'Option 1 exists'); $this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists'); $this->assertTrue(!empty($form[$this->fieldName]['widget'][3]), 'Option 3 exists'); // Use one of the values in an actual entity, and check that this value // cannot be removed from the list. $entity = entity_create('entity_test'); $entity->{$this->fieldName}->value = 1; $entity->save(); $this->fieldStorage->setSetting('allowed_values', [2 => 'Two']); try { $this->fieldStorage->save(); $this->fail(t('Cannot update a list field storage to not include keys with existing data.')); } catch (FieldStorageDefinitionUpdateForbiddenException $e) { $this->pass(t('Cannot update a list field storage to not include keys with existing data.')); } // Empty the value, so that we can actually remove the option. unset($entity->{$this->fieldName}); $entity->save(); // Removed options do not appear. $this->fieldStorage->setSetting('allowed_values', [2 => 'Two']); $this->fieldStorage->save(); $entity = entity_create('entity_test'); $form = \Drupal::service('entity.form_builder')->getForm($entity); $this->assertTrue(empty($form[$this->fieldName]['widget'][1]), 'Option 1 does not exist'); $this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists'); $this->assertTrue(empty($form[$this->fieldName]['widget'][3]), 'Option 3 does not exist'); // Completely new options appear. $this->fieldStorage->setSetting('allowed_values', [10 => 'Update', 20 => 'Twenty']); $this->fieldStorage->save(); // The entity holds an outdated field object with the old allowed values // setting, so we need to reinitialize the entity object. $entity = entity_create('entity_test'); $form = \Drupal::service('entity.form_builder')->getForm($entity); $this->assertTrue(empty($form[$this->fieldName]['widget'][1]), 'Option 1 does not exist'); $this->assertTrue(empty($form[$this->fieldName]['widget'][2]), 'Option 2 does not exist'); $this->assertTrue(empty($form[$this->fieldName]['widget'][3]), 'Option 3 does not exist'); $this->assertTrue(!empty($form[$this->fieldName]['widget'][10]), 'Option 10 exists'); $this->assertTrue(!empty($form[$this->fieldName]['widget'][20]), 'Option 20 exists'); // Options are reset when a new field with the same name is created. $this->fieldStorage->delete(); entity_create('field_storage_config', $this->fieldStorageDefinition)->save(); entity_create('field_config', array('field_name' => $this->fieldName, 'entity_type' => 'entity_test', 'bundle' => 'entity_test'))->save(); entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($this->fieldName, array('type' => 'options_buttons'))->save(); $entity = entity_create('entity_test'); $form = \Drupal::service('entity.form_builder')->getForm($entity); $this->assertTrue(!empty($form[$this->fieldName]['widget'][1]), 'Option 1 exists'); $this->assertTrue(!empty($form[$this->fieldName]['widget'][2]), 'Option 2 exists'); $this->assertTrue(!empty($form[$this->fieldName]['widget'][3]), 'Option 3 exists'); // Test the generateSampleValue() method. $entity = entity_create('entity_test'); $entity->{$this->fieldName}->generateSampleItems(); $this->entityValidateAndSave($entity); }
public function setUp() { parent::setUp(); // Create an email field and instance for validation. entity_create('field_storage_config', array('name' => 'field_email', 'entity_type' => 'entity_test', 'type' => 'email'))->save(); entity_create('field_instance_config', array('entity_type' => 'entity_test', 'field_name' => 'field_email', 'bundle' => 'entity_test'))->save(); // Create a form display for the default form mode. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent('field_email', array('type' => 'email_default'))->save(); }
/** * Create a new image field. * * @param string $name * The name of the new field (all lowercase), exclude the "field_" prefix. * @param string $type_name * The node type that this field will be added to. * @param array $storage_settings * A list of field storage settings that will be added to the defaults. * @param array $field_settings * A list of instance settings that will be added to the instance defaults. * @param array $widget_settings * A list of widget settings that will be added to the widget defaults. */ function createImageField($name, $type_name, $storage_settings = array(), $field_settings = array(), $widget_settings = array()) { entity_create('field_storage_config', array('field_name' => $name, 'entity_type' => 'node', 'type' => 'image', 'settings' => $storage_settings, 'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1))->save(); $field_config = entity_create('field_config', array('field_name' => $name, 'label' => $name, 'entity_type' => 'node', 'bundle' => $type_name, 'required' => !empty($field_settings['required']), 'settings' => $field_settings)); $field_config->save(); entity_get_form_display('node', $type_name, 'default')->setComponent($name, array('type' => 'image_image', 'settings' => $widget_settings))->save(); entity_get_display('node', $type_name, 'default')->setComponent($name)->save(); return $field_config; }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); // Create a boolean field and storage for validation. FieldStorageConfig::create(array('field_name' => 'field_boolean', 'entity_type' => 'entity_test', 'type' => 'boolean'))->save(); FieldConfig::create(['entity_type' => 'entity_test', 'field_name' => 'field_boolean', 'bundle' => 'entity_test'])->save(); // Create a form display for the default form mode. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent('field_boolean', array('type' => 'boolean_checkbox'))->save(); }
/** * Tests the Drupal 6 vocabulary-node type association to Drupal 8 migration. */ public function testVocabularyEntityFormDisplay() { // Test that the field exists. $component = entity_get_form_display('node', 'page', 'default')->getComponent('tags'); $this->assertIdentical('options_select', $component['type']); $this->assertIdentical(20, $component['weight']); // Test the Id map. $this->assertIdentical(array('node', 'article', 'default', 'tags'), entity_load('migration', 'd6_vocabulary_entity_form_display')->getIdMap()->lookupDestinationID(array(4, 'article'))); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); // Create a boolean field and storage for validation. entity_create('field_storage_config', array('field_name' => 'field_boolean', 'entity_type' => 'entity_test', 'type' => 'boolean'))->save(); entity_create('field_config', array('entity_type' => 'entity_test', 'field_name' => 'field_boolean', 'bundle' => 'entity_test'))->save(); // Create a form display for the default form mode. entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent('field_boolean', array('type' => 'boolean'))->save(); }
/** * Create a new image field. * * @param string $name * The name of the new field (all lowercase), exclude the "field_" prefix. * @param string $type_name * The node type that this field will be added to. * @param array $storage_settings * A list of field storage settings that will be added to the defaults. * @param array $field_settings * A list of instance settings that will be added to the instance defaults. * @param array $widget_settings * Widget settings to be added to the widget defaults. * @param array $formatter_settings * Formatter settings to be added to the formatter defaults. * @param string $description * A description for the field. */ function createImageField($name, $type_name, $storage_settings = array(), $field_settings = array(), $widget_settings = array(), $formatter_settings = array(), $description = '') { FieldStorageConfig::create(array('field_name' => $name, 'entity_type' => 'node', 'type' => 'image', 'settings' => $storage_settings, 'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1))->save(); $field_config = FieldConfig::create(['field_name' => $name, 'label' => $name, 'entity_type' => 'node', 'bundle' => $type_name, 'required' => !empty($field_settings['required']), 'settings' => $field_settings, 'description' => $description]); $field_config->save(); entity_get_form_display('node', $type_name, 'default')->setComponent($name, array('type' => 'image_image', 'settings' => $widget_settings))->save(); entity_get_display('node', $type_name, 'default')->setComponent($name, array('type' => 'image', 'settings' => $formatter_settings))->save(); return $field_config; }
/** * Posts a comment. * * @param \Drupal\Core\Entity\EntityInterface|null $entity * Entity to post comment on or NULL to post to the previously loaded page. * @param $comment * Comment body. * @param $subject * Comment subject. * @param $contact * Set to NULL for no contact info, TRUE to ignore success checking, and * array of values to set contact info. */ function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL) { $edit = array(); $edit['comment_body[0][value]'] = $comment; $instance = FieldInstanceConfig::loadByName('entity_test', 'entity_test', 'comment'); $preview_mode = $instance->getSetting('preview'); // Must get the page before we test for fields. if ($entity !== NULL) { $this->drupalGet('comment/reply/entity_test/' . $entity->id() . '/comment'); } // Determine the visibility of subject form field. if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) { // Subject input allowed. $edit['subject[0][value]'] = $subject; } else { $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); } if ($contact !== NULL && is_array($contact)) { $edit += $contact; } switch ($preview_mode) { case DRUPAL_REQUIRED: // Preview required so no save button should be found. $this->assertNoFieldByName('op', t('Save'), 'Save button not found.'); $this->drupalPostForm(NULL, $edit, t('Preview')); // Don't break here so that we can test post-preview field presence and // function below. // Don't break here so that we can test post-preview field presence and // function below. case DRUPAL_OPTIONAL: $this->assertFieldByName('op', t('Preview'), 'Preview button found.'); $this->assertFieldByName('op', t('Save'), 'Save button found.'); $this->drupalPostForm(NULL, $edit, t('Save')); break; case DRUPAL_DISABLED: $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.'); $this->assertFieldByName('op', t('Save'), 'Save button found.'); $this->drupalPostForm(NULL, $edit, t('Save')); break; } $match = array(); // Get comment ID preg_match('/#comment-([0-9]+)/', $this->getURL(), $match); // Get comment. if ($contact !== TRUE) { // If true then attempting to find error message. if ($subject) { $this->assertText($subject, 'Comment subject posted.'); } $this->assertText($comment, 'Comment body posted.'); $this->assertTrue(!empty($match) && !empty($match[1]), 'Comment ID found.'); } if (isset($match[1])) { return entity_load('comment', $match[1]); } }
/** * Adds term reference field for the article content type. * * @param bool $translatable * (optional) If TRUE, create a translatable term reference field. Defaults * to FALSE. */ protected function setUpTermReferenceField() { $handler_settings = array('target_bundles' => array($this->vocabulary->id() => $this->vocabulary->id()), 'auto_create' => TRUE); $this->createEntityReferenceField('node', 'article', $this->termFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); $field_storage = FieldStorageConfig::loadByName('node', $this->termFieldName); $field_storage->setTranslatable(FALSE); $field_storage->save(); entity_get_form_display('node', 'article', 'default')->setComponent($this->termFieldName, array('type' => 'entity_reference_autocomplete_tags'))->save(); entity_get_display('node', 'article', 'default')->setComponent($this->termFieldName, array('type' => 'entity_reference_label'))->save(); }
/** * Create taxonomy term reference field for testing categories. * * @param object $vocabulary * Taxonomy vocabulary. * * @return string * Created field name. */ protected function createTaxonomyTermReferenceField($vocabulary) { $field_tags_name = Unicode::strtolower($this->randomMachineName()); $field_storage = entity_create('field_storage_config', array('field_name' => $field_tags_name, 'entity_type' => 'node', 'type' => 'taxonomy_term_reference', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'settings' => array('allowed_values' => array(array('vocabulary' => $vocabulary->id(), 'parent' => '0'))))); $field_storage->save(); entity_create('field_config', array('field_storage' => $field_storage, 'bundle' => 'article'))->save(); entity_get_form_display('node', 'article', 'default')->setComponent($field_tags_name, array('type' => 'taxonomy_autocomplete'))->save(); entity_get_display('node', 'article', 'full')->setComponent($field_tags_name, array('type' => 'taxonomy_term_reference_link'))->save(); return $field_tags_name; }