本文整理汇总了PHP中Drupal\Core\Entity\Entity\EntityViewDisplay::create方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityViewDisplay::create方法的具体用法?PHP EntityViewDisplay::create怎么用?PHP EntityViewDisplay::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Entity\Entity\EntityViewDisplay
的用法示例。
在下文中一共展示了EntityViewDisplay::create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
/**
* {@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"]);
}
示例2: testEntityDisplayDependency
/**
* 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']);
}
示例3: createField
/**
* 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();
}
示例4: setUp
/**
* {@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);
}
示例5: setUp
/**
* {@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();
}
示例6: setUpFixtures
/**
* {@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();
}
示例7: testViewMode
/**
* 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]);
}
示例8: testEntityViewDisplayDependency
/**
* 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'));
}
示例9: testOnDependencyRemoval
/**
* 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));
}
示例10: getSingleFieldDisplay
/**
* 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;
}
示例11: testGetDisplayModeOptions
/**
* 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'));
}
示例12: setUp
/**
* {@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();
}
示例13: addParagraphsField
/**
* 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();
}
示例14: testEnableModulesFixedList
/**
* 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();
}
示例15: buildFields
/**
* 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;
}