/** * {@inheritdoc} */ public function build() { /** @var \Drupal\Core\Entity\EntityInterface $group */ $group = $this->getContext('og')->getContextValue(); $view_builder = $this->entityTypeManager->getViewBuilder($group->getEntityTypeId()); // We render the related view mode only in this block. Additional modules, // like collection and solution, will add more elements directly. $build['group'] = $view_builder->view($group, 'group_header'); return $build; }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { $view_mode = $this->getSetting('view_mode'); $elements = array(); foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) { // Protect ourselves from recursive rendering. static $depth = 0; $depth++; if ($depth > 20) { $this->loggerFactory->get('entity')->error('Recursive rendering detected when rendering entity @entity_type @entity_id. Aborting rendering.', array('@entity_type' => $entity->getEntityTypeId(), '@entity_id' => $entity->id())); return $elements; } if ($entity->id()) { $view_builder = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId()); $elements[$delta] = $view_builder->view($entity, $view_mode, $entity->language()->getId()); // Add a resource attribute to set the mapping property's value to the // entity's url. Since we don't know what the markup of the entity will // be, we shouldn't rely on it for structured data such as RDFa. if (!empty($items[$delta]->_attributes)) { $items[$delta]->_attributes += array('resource' => $entity->url()); } } else { // This is an "auto_create" item. $elements[$delta] = array('#markup' => $entity->label()); } $depth = 0; } return $elements; }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { $view_mode = $this->getSetting('view_mode'); $elements = array(); foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) { // Due to render caching and delayed calls, the viewElements() method // will be called later in the rendering process through a '#pre_render' // callback, so we need to generate a counter that takes into account // all the relevant information about this field and the referenced // entity that is being rendered. $recursive_render_id = $items->getFieldDefinition()->getTargetEntityTypeId() . $items->getFieldDefinition()->getTargetBundle() . $items->getName() . $entity->id(); if (isset(static::$recursiveRenderDepth[$recursive_render_id])) { static::$recursiveRenderDepth[$recursive_render_id]++; } else { static::$recursiveRenderDepth[$recursive_render_id] = 1; } // Protect ourselves from recursive rendering. if (static::$recursiveRenderDepth[$recursive_render_id] > static::RECURSIVE_RENDER_LIMIT) { $this->loggerFactory->get('entity')->error('Recursive rendering detected when rendering entity %entity_type: %entity_id, using the %field_name field on the %bundle_name bundle. Aborting rendering.', ['%entity_type' => $entity->getEntityTypeId(), '%entity_id' => $entity->id(), '%field_name' => $items->getName(), '%bundle_name' => $items->getFieldDefinition()->getTargetBundle()]); return $elements; } $view_builder = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId()); $elements[$delta] = $view_builder->view($entity, $view_mode, $entity->language()->getId()); // Add a resource attribute to set the mapping property's value to the // entity's url. Since we don't know what the markup of the entity will // be, we shouldn't rely on it for structured data such as RDFa. if (!empty($items[$delta]->_attributes) && !$entity->isNew() && $entity->hasLinkTemplate('canonical')) { $items[$delta]->_attributes += array('resource' => $entity->toUrl()->toString()); } } return $elements; }
/** * Preview job item entity data. * * @param \Drupal\tmgmt\JobItemInterface $tmgmt_job_item * Job item to be previewed. * @param string $view_mode * The view mode that should be used to display the entity. * * @return array * A render array as expected by drupal_render(). */ public function preview(JobItemInterface $tmgmt_job_item, $view_mode) { // Load entity. $entity = $this->entityTypeManager->getStorage($tmgmt_job_item->getItemType())->load($tmgmt_job_item->getItemId()); // We cannot show the preview for non-existing entities. if (!$entity) { throw new NotFoundHttpException(); } $data = $tmgmt_job_item->getData(); $target_langcode = $tmgmt_job_item->getJob()->getTargetLangcode(); // Populate preview with target translation data. $preview = $this->makePreview($entity, $data, $target_langcode); // Build view for entity. $page = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId())->view($preview, $view_mode, $preview->language()->getId()); // The preview is not cacheable. $page['#cache']['max-age'] = 0; return $page; }
/** * Returns the render array for an entity. * * @param string $entity_type * The entity type. * @param mixed $id * The ID of the entity to render. * @param string $view_mode * (optional) The view mode that should be used to render the entity. * @param string $langcode * (optional) For which language the entity should be rendered, defaults to * the current content language. * * @return null|array * A render array for the entity or NULL if the entity does not exist. */ public function drupalEntity($entity_type, $id = NULL, $view_mode = NULL, $langcode = NULL) { $entity = $id ? $this->entityTypeManager->getStorage($entity_type)->load($id) : $this->routeMatch->getParameter($entity_type); if ($entity) { $render_controller = $this->entityTypeManager->getViewBuilder($entity_type); return $render_controller->view($entity, $view_mode, $langcode); } return NULL; }
/** * {@inheritdoc} */ public function build() { // Load one advertisement node. $nids = $this->entityQuery->get('node')->condition('type', 'ad')->range(0, 1)->condition('status', 1)->condition('field_ad_expiration', REQUEST_TIME, '>')->sort('created', 'DESC')->execute(); // Quick and dirty exit if no ad was created. if (empty($nids)) { return 'Create an Ad first.'; } // Build the ad teaser. /** @var \Drupal\node\Entity\Node $node */ $node = $this->entityTypeManager->getStorage('node')->load(reset($nids)); $teaser = $this->entityTypeManager->getViewBuilder('node')->view($node, 'teaser'); $build['ad'] = $teaser; $build['cart_link'] = array('#type' => 'link', '#url' => Url::fromRoute('cacheit.shopping_cart', array(), ['query' => ['product_id' => $node->id()]]), '#title' => t('Add to cart'), '#attributes' => ['class' => ['button']], '#weight' => 10); $build['conditions_link'] = array('#type' => 'link', '#url' => Url::fromUri('entity:node/2'), '#title' => t('Conditions'), '#weight' => 11, '#cache' => ['tags' => ['node:2'], 'max-age' => 3600]); // Dynamic content added using a placeholder. $build['validity'] = ['#lazy_builder' => ['cacheit.lazy_builders:renderAdValidity', [$node->id()]], '#create_placeholder' => TRUE]; return $build; }
/** * Generate the HTML for our entity. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity we're rendering. * @param bool $use_default_css * TRUE if we should inject our default CSS otherwise FALSE. * @param bool $optimize_css * TRUE if we should compress the CSS otherwise FALSE. * * @return string * The generated HTML. * * @throws \Exception */ protected function getHtml(ContentEntityInterface $entity, $use_default_css, $optimize_css) { $render_controller = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId()); $render = ['#theme' => 'entity_print__' . $entity->getEntityTypeId() . '__' . $entity->id(), '#entity' => $entity, '#entity_array' => $render_controller->view($entity, 'pdf'), '#attached' => []]; // Inject some generic CSS across all templates. if ($use_default_css) { $render['#attached']['library'][] = 'entity_print/default'; } // Allow other modules to add their own CSS. $this->moduleHandler->alter('entity_print_css', $render, $entity); // Inject CSS from the theme info files and then render the CSS. $render = $this->addCss($render, $entity); $css_assets = $this->assetResolver->getCssAssets(AttachedAssets::createFromRenderArray($render), $optimize_css); $rendered_css = $this->cssRenderer->render($css_assets); $render['#entity_print_css'] = $this->renderer->render($rendered_css); return $this->renderer->render($render); }