getBuildDefaults example

$build_list = [
      '#sorted' => TRUE,
      '#pre_render' => [[$this, 'buildMultiple']],
    ];
    $weight = 0;
    foreach ($entities as $key => $entity) {
      // Ensure that from now on we are dealing with the proper translation       // object.       $entity = $this->entityRepository->getTranslationFromContext($entity$langcode);

      // Set build defaults.       $build_list[$key] = $this->getBuildDefaults($entity$view_mode);
      $entityType = $this->entityTypeId;
      $this->moduleHandler()->alter([$entityType . '_build_defaults', 'entity_build_defaults']$build_list[$key]$entity$view_mode);

      $build_list[$key]['#weight'] = $weight++;
    }

    return $build_list;
  }

  /** * Provides entity-specific defaults to the build process. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity for which the defaults should be provided. * @param string $view_mode * The view mode that should be used. * * @return array */
$container->get('current_user'),
      $container->get('theme.registry'),
      $container->get('entity_display.repository'),
      $container->get('entity_type.manager')
    );
  }

  /** * {@inheritdoc} */
  protected function getBuildDefaults(EntityInterface $entity$view_mode) {
    $build = parent::getBuildDefaults($entity$view_mode);

    /** @var \Drupal\comment\CommentInterface $entity */
    // Store a threading field setting to use later in self::buildComponents().     $commented_entity = $entity->getCommentedEntity();
    $build['#comment_threaded'] =
      is_null($commented_entity)
      || $commented_entity->getFieldDefinition($entity->getFieldName())
        ->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED;
    // If threading is enabled, don't render cache individual comments, but do     // keep the cacheability metadata, so it can bubble up.     if ($build['#comment_threaded']) {
      
use Drupal\Core\Render\Element;

/** * Render controller for contact messages. */
class MessageViewBuilder extends EntityViewBuilder {

  /** * {@inheritdoc} */
  protected function getBuildDefaults(EntityInterface $entity$view_mode) {
    $build = parent::getBuildDefaults($entity$view_mode);
    // The message fields are individually rendered into email templates, so     // the entity has no template itself.     unset($build['#theme']);
    return $build;
  }

  /** * {@inheritdoc} */
  public function view(EntityInterface $entity$view_mode = 'full', $langcode = NULL) {
    $build = parent::view($entity$view_mode$langcode);

    
'#prefix' => '<div id="field-language-display">',
          '#suffix' => '</div>',
        ];
      }
    }
  }

  /** * {@inheritdoc} */
  protected function getBuildDefaults(EntityInterface $entity$view_mode) {
    $defaults = parent::getBuildDefaults($entity$view_mode);

    // Don't cache nodes that are in 'preview' mode.     if (isset($defaults['#cache']) && isset($entity->in_preview)) {
      unset($defaults['#cache']);
    }

    return $defaults;
  }

  /** * #lazy_builder callback; builds a node's links. * * @param string $node_entity_id * The node entity ID. * @param string $view_mode * The view mode in which the node entity is being viewed. * @param string $langcode * The language in which the node entity is being viewed. * @param bool $is_in_preview * Whether the node is currently being previewed. * @param $revision_id * (optional) The identifier of the node revision to be loaded. If none * is provided, the default revision will be loaded. * * @return array * A renderable array representing the node links. */
// Apply the buildMultiple() #pre_render callback immediately, to make     // bubbling of attributes and contextual links to the actual block work.     // @see \Drupal\block\BlockViewBuilder::buildBlock()     unset($build_list['#pre_render'][0]);
    return $this->buildMultiple($build_list);
  }

  /** * {@inheritdoc} */
  protected function getBuildDefaults(EntityInterface $entity$view_mode) {
    $build = parent::getBuildDefaults($entity$view_mode);
    // The content block will be rendered in the wrapped block template already     // and thus has no entity template itself.     unset($build['#theme']);
    return $build;
  }

}
Home | Imprint | This part of the site doesn't use cookies.