/** * {@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']); }
/** * Creates a date test field. */ protected function createField() { $field_name = Unicode::strtolower($this->randomMachineName()); $type = $this->getTestFieldType(); $widget_type = $formatter_type = $type . '_default'; $this->fieldStorage = FieldStorageConfig::create(['field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => $type, 'settings' => ['datetime_type' => DateRangeItem::DATETIME_TYPE_DATE]]); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_storage' => $this->fieldStorage, 'bundle' => 'entity_test', 'required' => TRUE]); $this->field->save(); EntityFormDisplay::load('entity_test.entity_test.default')->setComponent($field_name, ['type' => $widget_type])->save(); $this->displayOptions = ['type' => $formatter_type, 'label' => 'hidden', 'settings' => ['format_type' => 'medium'] + $this->defaultSettings]; EntityViewDisplay::create(['targetEntityType' => $this->field->getTargetEntityTypeId(), 'bundle' => $this->field->getTargetBundle(), 'mode' => 'full', 'status' => TRUE])->setComponent($field_name, $this->displayOptions)->save(); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema($this->entityTypeId); EntityViewDisplay::create(['targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'default'])->save(); FieldStorageConfig::create(['field_name' => $this->fieldName, 'entity_type' => $this->entityTypeId, 'type' => 'video_embed_field'])->save(); FieldConfig::create(['entity_type' => $this->entityTypeId, 'field_name' => $this->fieldName, 'bundle' => $this->entityTypeId])->save(); // Fake colorbox being enabled for the purposes of testing. $this->container->get('module_handler')->addModule('colorbox', NULL); // Use a HTTP mock which won't attempt to download anything. $this->container->set('http_client', new MockHttpClient()); // Shim in a service required from the colorbox module. $colorbox_mock = $this->getMockBuilder('ColorboxAttachment')->setMethods(['attach'])->getMock(); $this->container->set('colorbox.attachment', $colorbox_mock); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema('entity_test'); $this->installEntitySchema('user'); $this->installConfig(['system']); $this->installSchema('system', ['sequences', 'key_value']); // Add a datetime range field. $this->fieldStorage = FieldStorageConfig::create(['field_name' => Unicode::strtolower($this->randomMachineName()), 'entity_type' => 'entity_test', 'type' => 'daterange', 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE]]); $this->fieldStorage->save(); $this->field = FieldConfig::create(['field_storage' => $this->fieldStorage, 'bundle' => 'entity_test', 'required' => TRUE]); $this->field->save(); $display_options = ['type' => 'daterange_default', 'label' => 'hidden', 'settings' => ['format_type' => 'fallback', 'separator' => 'UNTRANSLATED']]; EntityViewDisplay::create(['targetEntityType' => $this->field->getTargetEntityTypeId(), 'bundle' => $this->field->getTargetBundle(), 'mode' => 'default', 'status' => TRUE])->setComponent($this->fieldStorage->getName(), $display_options)->save(); }
/** * {@inheritdoc} */ protected function setUpFixtures() { $this->installEntitySchema('user'); $this->installEntitySchema('entity_test'); $this->installConfig(['entity_test']); EntityViewMode::create(['id' => 'entity_test.foobar', 'targetEntityType' => 'entity_test', 'status' => TRUE, 'enabled' => TRUE, 'label' => 'My view mode'])->save(); $display = EntityViewDisplay::create(['targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'foobar', 'label' => 'My view mode', 'status' => TRUE]); $display->save(); $field_storage = FieldStorageConfig::create(['field_name' => 'test_field', 'entity_type' => 'entity_test', 'type' => 'string']); $field_storage->save(); $field_config = FieldConfig::create(['field_name' => 'test_field', 'entity_type' => 'entity_test', 'bundle' => 'entity_test']); $field_config->save(); // Create some test entities. for ($i = 1; $i <= 3; $i++) { EntityTest::create(['name' => "Article title {$i}", 'test_field' => "Test {$i}"])->save(); } $this->user = User::create(['name' => 'test user']); $this->user->save(); parent::setUpFixtures(); }
/** * Tests view mode setting integration. * * @see comment_entity_view_display_presave() * @see CommentDefaultFormatter::calculateDependencies() */ public function testViewMode() { $mode = Unicode::strtolower($this->randomMachineName()); // Create a new comment view mode and a view display entity. EntityViewMode::create(['id' => "comment.{$mode}", 'targetEntityType' => 'comment', 'settings' => ['comment_type' => 'comment']])->save(); EntityViewDisplay::create(['targetEntityType' => 'comment', 'bundle' => 'comment', 'mode' => $mode])->setStatus(TRUE)->save(); // Create a comment field attached to a host 'entity_test' entity. FieldStorageConfig::create(['entity_type' => 'entity_test', 'type' => 'comment', 'field_name' => $field_name = Unicode::strtolower($this->randomMachineName()), 'settings' => ['comment_type' => 'comment']])->save(); FieldConfig::create(['entity_type' => 'entity_test', 'bundle' => 'entity_test', 'field_name' => $field_name])->save(); $component = ['type' => 'comment_default', 'settings' => ['view_mode' => $mode, 'pager_id' => 0]]; // Create a new 'entity_test' view display on host entity that uses the // custom comment display in field formatter to show the field. EntityViewDisplay::create(['targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'default'])->setComponent($field_name, $component)->setStatus(TRUE)->save(); $host_display_id = 'entity_test.entity_test.default'; $comment_display_id = "comment.comment.{$mode}"; // Disable the "comment.comment.$mode" display. EntityViewDisplay::load($comment_display_id)->setStatus(FALSE)->save(); /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $host_display */ $host_display = EntityViewDisplay::load($host_display_id); // Check that the field formatter has been disabled on host view display. $this->assertNull($host_display->getComponent($field_name)); $this->assertTrue($host_display->get('hidden')[$field_name]); // Check that the proper warning has been logged. $arguments = ['@id' => $host_display_id, '@name' => $field_name, '@display' => EntityViewMode::load("comment.{$mode}")->label(), '@mode' => $mode]; $logged = (bool) Database::getConnection()->select('watchdog')->fields('watchdog', ['wid'])->condition('type', 'system')->condition('message', "View display '@id': Comment field formatter '@name' was disabled because it is using the comment view display '@display' (@mode) that was just disabled.")->condition('variables', serialize($arguments))->execute()->fetchField(); $this->assertTrue($logged); // Re-enable the comment view display. EntityViewDisplay::load($comment_display_id)->setStatus(TRUE)->save(); // Re-enable the comment field formatter on host entity view display. EntityViewDisplay::load($host_display_id)->setComponent($field_name, $component)->save(); // Delete the "comment.$mode" view mode. EntityViewMode::load("comment.{$mode}")->delete(); // Check that the comment view display entity has been deleted too. $this->assertNull(EntityViewDisplay::load($comment_display_id)); /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */ $host_display = EntityViewDisplay::load($host_display_id); // Check that the field formatter has been disabled on host view display. $this->assertNull($host_display->getComponent($field_name)); $this->assertTrue($host_display->get('hidden')[$field_name]); }
/** * Tests integration with entity view display. */ public function testEntityViewDisplayDependency() { // Create a responsive image style. ResponsiveImageStyle::create(['id' => 'foo', 'label' => 'Foo', 'breakpoint_group' => 'responsive_image_test_module'])->save(); // Create an image field to be used with a responsive image formatter. FieldStorageConfig::create(['type' => 'image', 'entity_type' => 'entity_test', 'field_name' => 'bar'])->save(); FieldConfig::create(['entity_type' => 'entity_test', 'bundle' => 'entity_test', 'field_name' => 'bar'])->save(); /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */ $display = EntityViewDisplay::create(['targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'default']); $display->setComponent('bar', ['type' => 'responsive_image', 'label' => 'hidden', 'settings' => ['responsive_image_style' => 'foo', 'image_link' => ''], 'third_party_settings' => []])->save(); // Check that the 'foo' field is on the display. $this->assertNotNull($display = EntityViewDisplay::load('entity_test.entity_test.default')); $this->assertTrue($display->getComponent('bar')); $this->assertArrayNotHasKey('bar', $display->get('hidden')); // Delete the responsive image style. ResponsiveImageStyle::load('foo')->delete(); // Check that the view display was not deleted. $this->assertNotNull($display = EntityViewDisplay::load('entity_test.entity_test.default')); // Check that the 'foo' field was disabled. $this->assertNull($display->getComponent('bar')); $this->assertArrayHasKey('bar', $display->get('hidden')); }
/** * 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(); EntityViewDisplay::create(array('targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'default'))->setComponent($field_name, array('type' => 'field_plugins_test_text_formatter'))->save(); // Check the component exists and is of the correct type. $display = entity_get_display('entity_test', 'entity_test', 'default'); $this->assertEqual($display->getComponent($field_name)['type'], 'field_plugins_test_text_formatter'); // Removing the field_plugins_test module should change the component to use // the default formatter for test fields. \Drupal::service('config.manager')->uninstall('module', 'field_plugins_test'); $display = entity_get_display('entity_test', 'entity_test', 'default'); $this->assertEqual($display->getComponent($field_name)['type'], 'text_default'); // Removing the text module should remove the field from the view display. \Drupal::service('config.manager')->uninstall('module', 'text'); $display = entity_get_display('entity_test', 'entity_test', 'default'); $this->assertFalse($display->getComponent($field_name)); }
/** * Returns an EntityViewDisplay for rendering an individual field. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * @param string $field_name * The field name. * @param string|array $display_options * The display options passed to the viewField() method. * * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface */ protected function getSingleFieldDisplay($entity, $field_name, $display_options) { if (is_string($display_options)) { // View mode: use the Display configured for the view mode. $view_mode = $display_options; $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode); // Hide all fields except the current one. foreach (array_keys($entity->getFieldDefinitions()) as $name) { if ($name != $field_name) { $display->removeComponent($name); } } } else { // Array of custom display options: use a runtime Display for the // '_custom' view mode. Persist the displays created, to reduce the number // of objects (displays and formatter plugins) created when rendering a // series of fields individually for cases such as views tables. $entity_type_id = $entity->getEntityTypeId(); $bundle = $entity->bundle(); $key = $entity_type_id . ':' . $bundle . ':' . $field_name . ':' . crc32(serialize($display_options)); if (!isset($this->singleFieldDisplays[$key])) { $this->singleFieldDisplays[$key] = EntityViewDisplay::create(array('targetEntityType' => $entity_type_id, 'bundle' => $bundle, 'status' => TRUE))->setComponent($field_name, $display_options); } $display = $this->singleFieldDisplays[$key]; } return $display; }
/** * 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')); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema($this->entityTypeId); $this->installEntitySchema('filter_format'); // Setup a field and an entity display. EntityViewDisplay::create([ 'targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'default', ])->save(); FieldStorageConfig::create([ 'field_name' => $this->fieldName, 'entity_type' => $this->entityTypeId, 'type' => 'text', ])->save(); FieldConfig::create([ 'entity_type' => $this->entityTypeId, 'field_name' => $this->fieldName, 'bundle' => $this->entityTypeId, ])->save(); $this->entityViewDisplay = EntityViewDisplay::load('entity_test.entity_test.default'); // Create a test entity with a test value. $this->entity = EntityTest::create(); $this->entity->{$this->fieldName}->value = 'lorem ipsum'; $this->entity->save(); // Set the default filter format. FilterFormat::create([ 'format' => 'test_format', 'name' => $this->randomMachineName(), ])->save(); $this->container->get('config.factory') ->getEditable('filter.settings') ->set('fallback_format', 'test_format') ->save(); }
/** * 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(); }
/** * Tests that the module list is retained after enabling/installing/disabling. */ function testEnableModulesFixedList() { // Install system module. $this->container->get('module_installer')->install(array('system', 'menu_link_content')); $entity_manager = \Drupal::entityManager(); // entity_test is loaded via $modules; its entity type should exist. $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE); $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test')); // Load some additional modules; entity_test should still exist. $this->enableModules(array('field', 'text', 'entity_test')); $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE); $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test')); // Install some other modules; entity_test should still exist. $this->container->get('module_installer')->install(array('user', 'field', 'field_test'), FALSE); $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE); $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test')); // Uninstall one of those modules; entity_test should still exist. $this->container->get('module_installer')->uninstall(array('field_test')); $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE); $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test')); // Set the weight of a module; entity_test should still exist. module_set_weight('field', -1); $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE); $this->assertTrue(TRUE == $entity_manager->getDefinition('entity_test')); // Reactivate the previously uninstalled module. $this->enableModules(array('field_test')); // Create a field. $display = EntityViewDisplay::create(array('targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'default')); $field_storage = FieldStorageConfig::create(array('field_name' => 'test_field', 'entity_type' => 'entity_test', 'type' => 'test_field')); $field_storage->save(); FieldConfig::create(['field_storage' => $field_storage, 'bundle' => 'entity_test'])->save(); }
/** * Builds the render arrays for all fields of all result rows. * * The output is built using EntityViewDisplay objects to leverage * multiple-entity building and ensure a common code path with regular entity * view. * - Each relationship is handled by a separate EntityFieldRenderer instance, * since it operates on its own set of entities. This also ensures different * entity types are handled separately, as they imply different * relationships. * - Within each relationship, the fields to render are arranged in unique * sets containing each field at most once (an EntityViewDisplay can * only process a field once with given display options, but a View can * contain the same field several times with different display options). * - For each set of fields, entities are processed by bundle, so that * formatters can operate on the proper field definition for the bundle. * * @param \Drupal\views\ResultRow[] $values * An array of all ResultRow objects returned from the query. * * @return array * A renderable array for the fields handled by this renderer. * * @see \Drupal\Core\Entity\Entity\EntityViewDisplay */ protected function buildFields(array $values) { $build = []; if ($values && ($field_ids = $this->getRenderableFieldIds())) { $entity_type_id = $this->getEntityTypeId(); // Collect the entities for the relationship, fetch the right translation, // and group by bundle. For each result row, the corresponding entity can // be obtained from any of the fields handlers, so we arbitrarily use the // first one. $entities_by_bundles = []; $field = $this->view->field[current($field_ids)]; foreach ($values as $result_row) { $entity = $field->getEntity($result_row); $entities_by_bundles[$entity->bundle()][$result_row->index] = $this->getEntityTranslation($entity, $result_row); } // Determine unique sets of fields that can be processed by the same // display. Fields that appear several times in the View open additional // "overflow" displays. $display_sets = []; foreach ($field_ids as $field_id) { $field = $this->view->field[$field_id]; $index = 0; while (isset($display_sets[$index][$field->definition['field_name']])) { $index++; } $display_sets[$index][$field_id] = $field; } // For each set of fields, build the output by bundle. foreach ($display_sets as $display_fields) { foreach ($entities_by_bundles as $bundle => $bundle_entities) { // Create the display, and configure the field display options. $display = EntityViewDisplay::create(['targetEntityType' => $entity_type_id, 'bundle' => $bundle, 'status' => TRUE]); foreach ($display_fields as $field_id => $field) { $display->setComponent($field->definition['field_name'], ['type' => $field->options['type'], 'settings' => $field->options['settings']]); } // Let the display build the render array for the entities. $display_build = $display->buildMultiple($bundle_entities); // Collect the field render arrays and index them using our internal // row indexes and field IDs. foreach ($display_build as $row_index => $entity_build) { foreach ($display_fields as $field_id => $field) { $build[$row_index][$field_id] = !empty($entity_build[$field->definition['field_name']]) ? $entity_build[$field->definition['field_name']] : []; } } } } } return $build; }
/** * Tests the file_size field formatter. */ public function testFormatterFileSize() { $entity_display = EntityViewDisplay::create(['targetEntityType' => 'file', 'bundle' => 'file']); $entity_display->setComponent('filesize', ['type' => 'file_size']); $expected = ['10 bytes', '200 bytes', '39.06 KB', '7.63 MB']; foreach (array_values($this->files) as $i => $file) { $build = $entity_display->build($file); $this->assertEqual($expected[$i], $build['filesize'][0]['#markup']); } }
/** * {@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)); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); // Setup the rss view display. EntityViewDisplay::create(['status' => TRUE, 'targetEntityType' => 'node', 'bundle' => 'article', 'mode' => 'rss', 'content' => ['links' => ['weight' => 100]]])->save(); }
/** * Tests the comment formatter configured with a custom comment view mode. */ public function testViewMode() { $this->drupalLogin($this->webUser); $this->drupalGet($this->node->toUrl()); $comment_text = $this->randomMachineName(); // Post a comment. $this->postComment($this->node, $comment_text); // Comment displayed in 'default' display mode found and has body text. $comment_element = $this->cssSelect('.comment-wrapper'); $this->assertTrue(!empty($comment_element)); $this->assertRaw('<p>' . $comment_text . '</p>'); // Create a new comment entity view mode. $mode = Unicode::strtolower($this->randomMachineName()); EntityViewMode::create(['targetEntityType' => 'comment', 'id' => "comment.{$mode}"])->save(); // Create the corresponding entity view display for article node-type. Note // that this new view display mode doesn't contain the comment body. EntityViewDisplay::create(['targetEntityType' => 'comment', 'bundle' => 'comment', 'mode' => $mode])->setStatus(TRUE)->save(); /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $node_display */ $node_display = EntityViewDisplay::load('node.article.default'); $formatter = $node_display->getComponent('comment'); // Change the node comment field formatter to use $mode mode instead of // 'default' mode. $formatter['settings']['view_mode'] = $mode; $node_display->setComponent('comment', $formatter)->save(); // Reloading the node page to show the same node with its same comment but // with a different display mode. $this->drupalGet($this->node->toUrl()); // The comment should exist but without the body text because we used $mode // mode this time. $comment_element = $this->cssSelect('.comment-wrapper'); $this->assertTrue(!empty($comment_element)); $this->assertNoRaw('<p>' . $comment_text . '</p>'); }