/** * 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']; } }
/** * Act on entities of a particular type being assembled before rendering. * * @param &$build * A renderable array representing the entity content. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object. * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display * The entity view display holding the display options configured for the * entity components. * @param $view_mode * The view mode the entity is rendered in. * @param $langcode * The language code used for rendering. * * The module may add elements to $build prior to rendering. The * structure of $build is a renderable array as expected by * drupal_render(). * * @see hook_ENTITY_TYPE_view_alter() * @see hook_entity_view() * * @ingroup entity_crud */ function hook_ENTITY_TYPE_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode, $langcode) { // Only do the extra work if the component is configured to be displayed. // This assumes a 'mymodule_addition' extra field has been defined for the // entity bundle in hook_entity_extra_field_info(). if ($display->getComponent('mymodule_addition')) { $build['mymodule_addition'] = array('#markup' => mymodule_addition($entity), '#theme' => 'mymodule_my_additional_field'); } }