You are here

public function SocialPageTitleBlock::build in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()
  2. 8 modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()
  3. 8.2 modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()
  4. 8.3 modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()
  5. 8.4 modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()
  6. 8.6 modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()
  7. 8.7 modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()
  8. 8.8 modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()
  9. 10.3.x modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()
  10. 10.0.x modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()
  11. 10.1.x modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()
  12. 10.2.x modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php \Drupal\social_core\Plugin\Block\SocialPageTitleBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides PageTitleBlock::build

See also

\Drupal\block\BlockViewBuilder

File

modules/social_features/social_core/src/Plugin/Block/SocialPageTitleBlock.php, line 100

Class

SocialPageTitleBlock
Provides a 'SocialPageTitleBlock' block.

Namespace

Drupal\social_core\Plugin\Block

Code

public function build() {

  // Take the raw parameter. We'll load it ourselves.
  $nid = $this->routeMatch
    ->getRawParameter('node');
  $node = FALSE;
  $current_url = Url::fromRoute('<current>');
  $current_path = $current_url
    ->toString();

  // At this point the parameter could also be a simple string of a nid.
  // EG: on: /node/%node/enrollments.
  if (!is_null($nid) && !is_object($nid)) {
    $node = Node::load($nid);
  }
  $request = $this->requestStack
    ->getCurrentRequest();
  if ($node) {

    // Landing pages have their own heroes. Usually we're not displayed for
    // landing page. However, when a landing page is used as a 404 or 403 page
    // then this block is still rendered. Therefor if we're asked to render
    // for a landing page we check if we're not in a 404 or 403. If we are
    // then we can quickly determine we won't render anything.
    if ($node
      ->getType() === "landing_page") {
      $exception = $request->attributes
        ->get('exception');
      if ($exception instanceof NotFoundHttpException || $exception instanceof AccessDeniedHttpException) {
        return [];
      }
    }
    $translation = $this->entityRepository
      ->getTranslationFromContext($node);
    if (!empty($translation)) {
      $node
        ->setTitle($translation
        ->getTitle());
    }
    $name = $this->routeMatch
      ->getRouteName();
    if (!in_array($name, [
      'entity.node.edit_form',
      'entity.node.delete_form',
      'entity.node.add_form',
    ])) {
      $title = $node
        ->getTitle();
      return [
        '#theme' => 'page_hero_data',
        '#title' => $title,
        '#node' => $node,
      ];
    }
    else {
      return [
        '#type' => 'page_title',
        '#title' => $this->title,
      ];
    }
  }
  else {
    if ($route = $request->attributes
      ->get(RouteObjectInterface::ROUTE_OBJECT)) {
      $title = $this->titleResolver
        ->getTitle($request, $route);
      return [
        '#type' => 'page_title',
        '#title' => $title,
      ];
    }
    else {
      return [
        '#type' => 'page_title',
        '#title' => '',
      ];
    }
  }
}