You are here

public function VarbaseCreateContent::build in Varbase Total Control Dashboard 8

Same name and namespace in other branches
  1. 8.6 src/Plugin/Block/VarbaseCreateContent.php \Drupal\varbase_total_control\Plugin\Block\VarbaseCreateContent::build()
  2. 8.5 src/Plugin/Block/VarbaseCreateContent.php \Drupal\varbase_total_control\Plugin\Block\VarbaseCreateContent::build()
  3. 9.0.x src/Plugin/Block/VarbaseCreateContent.php \Drupal\varbase_total_control\Plugin\Block\VarbaseCreateContent::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 BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/VarbaseCreateContent.php, line 26

Class

VarbaseCreateContent
Provides a 'Create content'.

Namespace

Drupal\varbase_total_control\Plugin\Block

Code

public function build() {
  $types = array_reverse(node_type_get_types());
  $links = [];
  $config = $this
    ->getConfiguration();
  $destination = drupal_get_destination();
  $options = [
    $destination,
  ];
  foreach ($types as $type => $object) {

    // Check against pane config for type.
    if (!array_key_exists($type, $config['total_control_admin_types_links']) || (isset($config['total_control_admin_types_links']) && $config['total_control_admin_types_links'][$type]) == $type) {

      // Check access, then add a link to create content.
      if (\Drupal::currentUser()
        ->hasPermission('create ' . $object
        ->get('type') . ' content')) {
        $link_options = [
          'attributes' => [
            'class' => [
              Html::cleanCssIdentifier(Unicode::strtolower($object
                ->get('type'))),
            ],
          ],
        ];
        $url = new Url('node.add', [
          'node_type' => $object
            ->get('type'),
          $options,
        ]);
        $url
          ->setOptions($link_options);
        $links[] = \Drupal::l($object
          ->get('name'), $url);
      }
    }
  }
  $links[] = \Drupal::l($this
    ->t('More...'), new Url('node.add_page', $options));
  $body_data = [
    '#theme' => 'item_list',
    '#list_type' => 'ul',
    '#items' => $links,
  ];
  return [
    '#type' => 'markup',
    '#markup' => drupal_render($body_data),
  ];
}