/** * {@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 cache tags from image formatters. */ function testImageFormatterCacheTags() { // Create a test entity with the image field set. $entity = EntityTest::create(['name' => $this->randomMachineName()]); $entity->{$this->fieldName}->generateSampleItems(2); $entity->save(); // Generate the render array to verify if the cache tags are as expected. $build = $this->display->build($entity); $this->assertEquals($entity->{$this->fieldName}[0]->entity->getCacheTags(), $build[$this->fieldName][0]['#cache']['tags'], 'First image cache tags is as expected'); $this->assertEquals($entity->{$this->fieldName}[1]->entity->getCacheTags(), $build[$this->fieldName][1]['#cache']['tags'], 'Second image cache tags is as expected'); }
/** * Tests boolean formatter output. */ public function testBooleanFormatter() { $data = []; $data[] = [0, [], 'Off']; $data[] = [1, [], 'On']; $format = ['format' => 'enabled-disabled']; $data[] = [0, $format, 'Disabled']; $data[] = [1, $format, 'Enabled']; $format = ['format' => 'unicode-yes-no']; $data[] = [1, $format, '✔']; $data[] = [0, $format, '✖']; $format = ['format' => 'custom', 'format_custom_false' => 'FALSE', 'format_custom_true' => 'TRUE']; $data[] = [0, $format, 'FALSE']; $data[] = [1, $format, 'TRUE']; foreach ($data as $test_data) { list($value, $settings, $expected) = $test_data; $component = $this->display->getComponent($this->fieldName); $component['settings'] = $settings; $this->display->setComponent($this->fieldName, $component); $entity = EntityTest::create([]); $entity->{$this->fieldName}->value = $value; // Verify that all HTML is escaped and newlines are retained. $this->renderEntityFields($entity, $this->display); $this->assertRaw($expected); } }
/** * Tests TimestampAgoFormatter. */ public function testTimestampAgoFormatter() { $data = []; foreach (array(1, 2, 3, 4, 5, 6) as $granularity) { $data[] = ['future_format' => '@interval hence', 'past_format' => '@interval ago', 'granularity' => $granularity]; } foreach ($data as $settings) { $future_format = $settings['future_format']; $past_format = $settings['past_format']; $granularity = $settings['granularity']; $request_time = \Drupal::requestStack()->getCurrentRequest()->server->get('REQUEST_TIME'); // Test a timestamp in the past $value = $request_time - 87654321; $expected = SafeMarkup::format($past_format, ['@interval' => \Drupal::service('date.formatter')->formatTimeDiffSince($value, ['granularity' => $granularity])]); $component = $this->display->getComponent($this->fieldName); $component['type'] = 'timestamp_ago'; $component['settings'] = $settings; $this->display->setComponent($this->fieldName, $component); $entity = EntityTest::create([]); $entity->{$this->fieldName}->value = $value; $this->renderEntityFields($entity, $this->display); $this->assertRaw($expected); // Test a timestamp in the future $value = $request_time + 87654321; $expected = SafeMarkup::format($future_format, ['@interval' => \Drupal::service('date.formatter')->formatTimeDiffUntil($value, ['granularity' => $granularity])]); $component = $this->display->getComponent($this->fieldName); $component['type'] = 'timestamp_ago'; $component['settings'] = $settings; $this->display->setComponent($this->fieldName, $component); $entity = EntityTest::create([]); $entity->{$this->fieldName}->value = $value; $this->renderEntityFields($entity, $this->display); $this->assertRaw($expected); } }
/** * Act on entities being assembled before rendering. * * This is a hook bridge. * * @see hook_entity_view() * @see EntityFieldManagerInterface::getExtraFields() */ public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { if (!$this->moderationInfo->isModeratableEntity($entity)) { return; } if (!$this->moderationInfo->isLatestRevision($entity)) { return; } /** @var ContentEntityInterface $entity */ if ($entity->isDefaultRevision()) { return; } $component = $display->getComponent('workbench_moderation_control'); if ($component) { $build['workbench_moderation_control'] = $this->formBuilder->getForm(EntityModerationForm::class, $entity); $build['workbench_moderation_control']['#weight'] = $component['weight']; } }
/** * Tests string formatter output. */ public function testStringFormatter() { $value = $this->randomString(); $value .= "\n\n<strong>" . $this->randomString() . '</strong>'; $value .= "\n\n" . $this->randomString(); $entity = EntityTestRev::create(array()); $entity->{$this->fieldName}->value = $value; // Verify that all HTML is escaped and newlines are retained. $this->renderEntityFields($entity, $this->display); $this->assertNoRaw($value); $this->assertRaw(nl2br(SafeMarkup::checkPlain($value))); // Verify the cache tags. $build = $entity->{$this->fieldName}->view(); $this->assertTrue(!isset($build[0]['#cache']), 'The string formatter has no cache tags.'); $value = $this->randomMachineName(); $entity->{$this->fieldName}->value = $value; $entity->save(); // Set the formatter to link to the entity. $this->display->setComponent($this->fieldName, ['type' => 'string', 'settings' => ['link_to_entity' => TRUE]]); $this->display->save(); $this->renderEntityFields($entity, $this->display); $this->assertLink($value, 0); $this->assertLinkByHref($entity->url()); // $entity->url('revision') falls back to the canonical URL if this is no // revision. $this->assertLinkByHref($entity->url('revision')); // Make the entity a new revision. $old_revision_id = $entity->getRevisionId(); $entity->setNewRevision(TRUE); $value2 = $this->randomMachineName(); $entity->{$this->fieldName}->value = $value2; $entity->save(); $entity_new_revision = \Drupal::entityManager()->getStorage('entity_test_rev')->loadRevision($old_revision_id); $this->renderEntityFields($entity, $this->display); $this->assertLink($value2, 0); $this->assertLinkByHref($entity->url('revision')); $this->renderEntityFields($entity_new_revision, $this->display); $this->assertLink($value, 0); $this->assertLinkByHref('/entity_test_rev/' . $entity_new_revision->id() . '/revision/' . $entity_new_revision->getRevisionId() . '/view'); }
/** * Tests string formatter output. */ public function testStringFormatter() { $value = $this->randomString(); $value .= "\n\n<strong>" . $this->randomString() . '</strong>'; $value .= "\n\n" . $this->randomString(); $entity = EntityTest::create(array()); $entity->{$this->fieldName}->value = $value; // Verify that all HTML is escaped and newlines are retained. $this->renderEntityFields($entity, $this->display); $this->assertNoRaw($value); $this->assertRaw(nl2br(String::checkPlain($value))); // Verify the cache tags. $build = $entity->{$this->fieldName}->view(); $this->assertTrue(!isset($build[0]['#cache']), format_string('The string formatter has no cache tags.')); // Set the formatter to link to the entity. $this->display->setComponent($this->fieldName, ['type' => 'string', 'settings' => ['link_to_entity' => TRUE]]); $this->display->save(); $value = $this->randomMachineName(); $entity->{$this->fieldName}->value = $value; $entity->save(); $this->renderEntityFields($entity, $this->display); $this->assertLink($value, 0); $this->assertLinkByHref($entity->url()); }
/** * Renders fields of a given entity with a given display. * * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The entity object with attached fields to render. * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display * The display to render the fields in. * * @return string * The rendered entity fields. */ protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) { $content = $display->build($entity); $content = $this->render($content); return $content; }
/** * Alter the settings used for displaying an entity. * * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display * The entity view display that will be used to display the entity * components. * @param array $context * An associative array containing: * - entity_type: The entity type, e.g., 'node' or 'user'. * - bundle: The bundle, e.g., 'page' or 'article'. * - view_mode: The view mode, e.g. 'full', 'teaser'... * * @ingroup entity_crud */ function hook_entity_view_display_alter(\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, array $context) { // Leave field labels out of the search index. if ($context['entity_type'] == 'node' && $context['view_mode'] == 'search_index') { foreach ($display->getComponents() as $name => $options) { if (isset($options['label'])) { $options['label'] = 'hidden'; $display->setComponent($name, $options); } } } }
/** * Get the Panels display out of an the entity view display * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display * The display. * @param $view_mode * The view mode. * * @return \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant * The Panels display. */ protected function getPanelsDisplay(EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { // First, check if the entity has the panelizer field. if (isset($entity->field_panelizer)) { $values = []; foreach ($entity->field_panelizer as $item) { $values[$item->view_mode] = $item->panels_display; } if (isset($values[$view_mode])) { return $this->panelsManager->importDisplay($values[$view_mode]); } } // Otherwise, get the correct default off the entity view display. $displays = $display->getThirdPartySetting('panelizer', 'displays', []); if (!empty($displays['default'])) { $displays['default'] = $this->panelsManager->importDisplay($displays['default']); } else { $displays['default'] = $this->getPanelizerPlugin()->getDefaultDisplay($display, $entity->bundle(), $view_mode); } return $displays['default']; }
/** * Set component settings for the display. * * @param string $type * The component to change settings for. * @param array $settings * The settings to use. */ protected function setDisplayComponentSettings($type, $settings = []) { $this->entityDisplay->setComponent($this->fieldName, ['type' => $type, 'settings' => $settings])->save(); }