You are here

public function DatexBlock::build in Datex 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

src/Plugin/Block/DatexBlock.php, line 105

Class

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

Namespace

Drupal\datex\Plugin\Block

Code

public function build() {
  $cfg = $this
    ->getConfiguration();
  $cfg = [
    'cal' => isset($cfg['cal']) ? $cfg['cal'] : 'persian',
    'fmt' => isset($cfg['fmt']) ? $cfg['fmt'] : 'Y/m/d H:i:s',
    'tz' => isset($cfg['tz']) ? $cfg['tz'] : 'user',
    'text' => isset($cfg['text']) ? $cfg['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 = \drupal_get_user_timezone();
      break;
    default:
      $tz = $cfg['tz'];
  }
  $calendar = datex_factory($tz, NULL, $cfg['cal']);
  $content = str_replace('{}', $calendar
    ->format($cfg['fmt']), $cfg['text']);
  return [
    '#markup' => $content,
  ];
}