You are here

public function ContactInfoBlock::build in Brainstorm profile 8

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 BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

module/custom/adci_contact_info/src/Plugin/Block/ContactInfoBlock.php, line 169

Class

ContactInfoBlock
Provides an contact info block.

Namespace

Drupal\adci_contact_info\Plugin\Block

Code

public function build() {
  $config = $this
    ->getConfiguration();
  $version = \Drupal::VERSION;
  $system_manager = \Drupal::service('system.manager');
  $requirements = $system_manager
    ->listRequirements();
  $severities = [];
  foreach ($requirements as $requirement) {
    if (isset($requirement['severity'])) {
      if (isset($severities[$requirement['severity']])) {
        $severities[(int) $requirement['severity']]++;
        continue;
      }
      $severities += [
        (int) $requirement['severity'] => 1,
      ];
    }
  }
  $listItems = [];
  if (isset($severities[$system_manager::REQUIREMENT_ERROR])) {
    $count = $severities[$system_manager::REQUIREMENT_ERROR];
    $listItems[] = [
      '#title' => $this
        ->formatPlural($count, '1 error', '@count errors'),
      '#type' => 'link',
      '#url' => Url::fromRoute('system.status'),
    ];
  }
  if (isset($severities[$system_manager::REQUIREMENT_WARNING])) {
    $count = $severities[$system_manager::REQUIREMENT_WARNING];
    $listItems[] = [
      '#title' => $this
        ->formatPlural($count, '1 warning', '@count warnings'),
      '#type' => 'link',
      '#url' => Url::fromRoute('system.status'),
    ];
  }
  $build['info'] = [
    '#type' => 'html_tag',
    '#tag' => 'p',
    '#value' => str_replace('@version', $version, $config['info']),
  ];
  if (!empty($config['site']['link'])) {
    $build['site'] = [
      '#type' => 'container',
    ];
    $build['site']['title'] = [
      '#type' => 'label',
      '#title' => $config['site']['title'],
      "#title_display" => 'before',
    ];
    $build['site']['link'] = [
      '#title' => $config['site']['text'],
      '#type' => 'link',
      '#url' => Url::fromUri($config['site']['link']),
      '#attributes' => [
        'target' => '_blank',
        'rel' => 'noopener noreferrer',
      ],
    ];
  }
  if (!empty($config['email']['email'])) {
    $build['email'] = [
      '#type' => 'container',
    ];
    $build['email']['title'] = [
      '#type' => 'label',
      '#title' => $config['email']['title'],
      "#title_display" => 'before',
    ];
    $build['email']['email'] = [
      '#type' => 'html_tag',
      '#tag' => 'a',
      '#value' => $config['email']['email'],
      '#attributes' => [
        'href' => 'mailto:' . $config['email']['email'],
      ],
    ];
  }
  if (count($listItems) > 0) {
    $build['errors'] = [
      '#theme' => 'item_list',
      '#list_type' => 'ul',
      '#title' => $this
        ->t('Your website requires your attention:'),
      '#items' => $listItems,
    ];
  }
  $build['#attributes'] = [
    'class' => [
      'adci-contact-info',
    ],
  ];
  if ($this->themeManager
    ->getActiveTheme()
    ->getName() === 'adminimal_theme') {
    $build['#attributes']['class'][] = 'adci-contact-info-adminimal';
  }
  $build['#attached'] = [
    'library' => [
      'adci_contact_info/main',
    ],
  ];
  return $build;
}