You are here

public function CalendarSystemsBlock::build in Calendar Systems 8.3

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

src/Plugin/Block/CalendarSystemsBlock.php, line 123

Class

CalendarSystemsBlock
Provides a block to show a localized current/relative date.

Namespace

Drupal\calendar_systems\Plugin\Block

Code

public function build() {
  $def = $this
    ->defaultConfiguration();
  $cfg = $this
    ->getConfiguration();
  $cfg = [
    'cal' => isset($cfg['calendar_systems_calendar']) ? $cfg['calendar_systems_calendar'] ?: $def['calendar_systems_calendar'] : $def['calendar_systems_calendar'],
    'fmt' => isset($cfg['calendar_systems_format']) ? $cfg['calendar_systems_format'] ?: $def['calendar_systems_format'] : $def['calendar_systems_format'],
    'tz' => isset($cfg['calendar_systems_timezone']) ? $cfg['calendar_systems_timezone'] ?: $def['calendar_systems_timezone'] : $def['calendar_systems_timezone'],
    'text' => isset($cfg['calendar_systems_text']) ? $cfg['calendar_systems_text'] ?: $def['calendar_systems_text'] : $def['calendar_systems_text'],
  ];
  switch ($cfg['tz']) {
    case 'site':
      $config = Drupal::config('system.date');
      $config_data_default_timezone = $config
        ->get('timezone.default');
      $tz = !empty($config_data_default_timezone) ? $config_data_default_timezone : @date_default_timezone_get();
      break;
    case 'user':
      $tz = date_default_timezone_get();
      break;
    default:
      $tz = $cfg['tz'];
  }
  if ($cfg['cal'] === 'global') {
    $cfg['cal'] = '';
  }
  $calendar = _calendar_systems_factory($tz, NULL, $cfg['cal']);
  $content = str_replace('{}', $calendar
    ->format($cfg['fmt']), $cfg['text']);
  return [
    '#markup' => $content,
  ];
}