/** * {@inheritdoc} */ public function view(EntityInterface $node, $view_mode = 'full', $langcode = NULL) { $build = parent::view($node, $view_mode, $langcode); foreach ($node->uriRelationships() as $rel) { $url = $node->toUrl($rel); // Add link relationships if the user is authenticated or if the anonymous // user has access. Access checking must be done for anonymous users to // avoid traffic to inaccessible pages from web crawlers. For // authenticated users, showing the links in HTML head does not impact // user experience or security, since the routes are access checked when // visited and only visible via view source. This prevents doing // potentially expensive and hard to cache access checks on every request. // This means that the page will vary by user.permissions. We also rely on // the access checking fallback to ensure the correct cacheability // metadata if we have to check access. if ($this->currentUser->isAuthenticated() || $url->access($this->currentUser)) { // Set the node path as the canonical URL to prevent duplicate content. $build['#attached']['html_head_link'][] = array(array('rel' => $rel, 'href' => $url->toString()), TRUE); } if ($rel == 'canonical') { // Set the non-aliased canonical path as a default shortlink. $build['#attached']['html_head_link'][] = array(array('rel' => 'shortlink', 'href' => $url->setOption('alias', TRUE)->toString()), TRUE); } } // Given this varies by $this->currentUser->isAuthenticated(), add a cache // context based on the anonymous role. $build['#cache']['contexts'][] = 'user.roles:anonymous'; return $build; }
/** * {@inheritdoc} */ public function view(EntityInterface $node_preview, $view_mode_id = 'full', $langcode = NULL) { $node_preview->preview_view_mode = $view_mode_id; $build = parent::view($node_preview, $view_mode_id); $build['#attached']['library'][] = 'node/drupal.node.preview'; // Don't render cache previews. unset($build['#cache']); return $build; }
/** * {@inheritdoc} */ public function view(EntityInterface $node, $view_mode = 'full', $langcode = NULL) { $build = parent::view($node, $view_mode, $langcode); foreach ($node->uriRelationships() as $rel) { // Set the node path as the canonical URL to prevent duplicate content. $build['#attached']['html_head_link'][] = array(array('rel' => $rel, 'href' => $node->url($rel)), TRUE); if ($rel == 'canonical') { // Set the non-aliased canonical path as a default shortlink. $build['#attached']['html_head_link'][] = array(array('rel' => 'shortlink', 'href' => $node->url($rel, array('alias' => TRUE))), TRUE); } } return $build; }
/** * Tests the enhancer method. * * @see \Drupal\Core\Entity\Controller\EntityViewController::view() */ public function testView() { // Mock a view builder. $render_controller = $this->getMockBuilder('Drupal\\entity_test\\EntityTestViewBuilder')->disableOriginalConstructor()->getMock(); $render_controller->expects($this->any())->method('view')->will($this->returnValue('Output from rendering the entity')); // Mock an entity manager. $entity_manager = $this->getMock('Drupal\\Core\\Entity\\EntityManagerInterface'); $entity_manager->expects($this->any())->method('getViewBuilder')->will($this->returnValue($render_controller)); // Mock the 'entity_test' entity type. $entity_type = $this->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityType')->disableOriginalConstructor()->getMock(); $entity_type->expects($this->once())->method('getKey')->with('label')->will($this->returnValue('name')); // Mock the 'name' field's definition. $field_definition = $this->getMock('Drupal\\Core\\Field\\FieldDefinition'); $field_definition->expects($this->any())->method('getDisplayOptions')->with('view')->will($this->returnValue(NULL)); // Mock an 'entity_test' entity. $entity = $this->getMockBuilder('Drupal\\entity_test\\Entity\\EntityTest')->disableOriginalConstructor()->getMock(); $entity->expects($this->once())->method('getEntityType')->will($this->returnValue($entity_type)); $entity->expects($this->any())->method('getFieldDefinition')->with('name')->will($this->returnValue($field_definition)); // Initialize the controller to test. $controller = new EntityViewController($entity_manager); // Test the view method. $this->assertEquals($controller->view($entity, 'full'), 'Output from rendering the entity'); }
/** * {@inheritdoc} */ public function view(EntityInterface $node_preview, $view_mode_id = 'full', $langcode = NULL) { $node_preview->preview_view_mode = $view_mode_id; $build = parent::view($node_preview, $view_mode_id); $build['#attached']['library'][] = 'node/drupal.node.preview'; // Don't render cache previews. unset($build['#cache']); foreach ($node_preview->uriRelationships() as $rel) { // Set the node path as the canonical URL to prevent duplicate content. $build['#attached']['html_head_link'][] = array(array('rel' => $rel, 'href' => $node_preview->url($rel)), TRUE); if ($rel == 'canonical') { // Set the non-aliased canonical path as a default shortlink. $build['#attached']['html_head_link'][] = array(array('rel' => 'shortlink', 'href' => $node_preview->url($rel, array('alias' => TRUE))), TRUE); } } return $build; }
/** * Displays a field collection item revision. * * @param int $field_collection_item_revision * The field collection item revision ID. * * @return array * An array suitable for drupal_render(). */ public function revisionShow($field_collection_item_revision) { $field_collection_item = $this->entityManager()->getStorage('field_collection_item')->loadRevision($field_collection_item_revision); $field_collection_item_view_controller = new EntityViewController($this->entityManager, \Drupal::service('renderer')); $page = $field_collection_item_view_controller->view($field_collection_item); unset($page['field_collection_item'][$field_collection_item->id()]['#cache']); return $page; }
/** * Provides a page to render a single entity revision. * * @param \Drupal\Core\Entity\EntityInterface $_entity_revision * The Entity to be rendered. Note this variable is named $_entity_revision * rather than $entity to prevent collisions with other named placeholders * in the route. * @param string $view_mode * (optional) The view mode that should be used to display the entity. * Defaults to 'full'. * * @return array * A render array. */ public function view(EntityInterface $_entity_revision, $view_mode = 'full') { return parent::view($_entity_revision, $view_mode); }