You are here

public function CreateContent::build in Total Control Admin Dashboard 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Block/CreateContent.php \Drupal\total_control\Plugin\Block\CreateContent::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/CreateContent.php, line 100

Class

CreateContent
Provides a 'Create content'.

Namespace

Drupal\total_control\Plugin\Block

Code

public function build() {
  $types = $this->entityTypeManager
    ->getStorage('node_type')
    ->loadMultiple();
  $links = [];
  $config = $this
    ->getConfiguration();
  $destination = $this->redirectDestination
    ->getAsArray();
  $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 ($this->currentUser
        ->hasPermission('create ' . $object
        ->get('type') . ' content')) {
        $links[] = Link::fromTextAndUrl($this
          ->t('Add new@space', [
          '@space' => ' ',
        ]) . $object
          ->get('name'), new Url('node.add', [
          'node_type' => $object
            ->get('type'),
          'options' => $options,
        ]))
          ->toString();
      }
    }
  }
  if (empty($links)) {
    $markup_data = $this
      ->t('No content types available.') . ' ' . Link::fromTextAndUrl($this
      ->t('Add content type'), new Url('node.type_add'))
      ->toString();
    return [
      '#type' => 'markup',
      '#markup' => $markup_data,
    ];
  }
  $body_data = [
    '#theme' => 'item_list',
    '#list_type' => 'ul',
    '#items' => $links,
  ];
  $markup_data = $this->renderer
    ->render($body_data);
  return [
    '#type' => 'markup',
    '#markup' => $markup_data,
  ];
}