You are here

public function AddContentByBundle::render in Add Content by Bundle Views Area Plugin 1.x

Render the area.

Parameters

bool $empty: (optional) Indicator if view result is empty or not. Defaults to FALSE.

Return value

array In any case we need a valid Drupal render array to return.

Overrides AreaPluginBase::render

File

src/Plugin/views/area/AddContentByBundle.php, line 187

Class

AddContentByBundle
Defines an area plugin to display a bundle-specific node/add link.

Namespace

Drupal\add_content_by_bundle\Plugin\views\area

Code

public function render($empty = FALSE) {
  $account = \Drupal::currentUser();
  if ($empty && empty($this->options['bundle'])) {
    return [];
  }
  $bundle_type = $this->options['bundle'];
  $entity_type = $this->view
    ->getBaseEntityType();

  // Assemble query params.
  $params = $this
    ->getDestinationArray();

  // If set, add form_mode to URL.
  if (\Drupal::service('module_handler')
    ->moduleExists('form_mode_control') && ($form_mode = $this->options['form_mode'])) {
    $params['display'] = $form_mode;
  }

  // If configured to add params, parse into our array.
  if ($this->options['params']) {
    $this
      ->extractParams($params, $this->options['params']);
  }

  // Try to be entity-type agnostic.
  if ($entity_type
    ->id() === 'node') {

    // Link to add a node of the specified type, then return to our view.
    $url = Url::fromRoute('node.add', [
      'node_type' => $bundle_type,
    ], [
      'query' => $params,
    ]);
    $access = $this->accessManager
      ->checkNamedRoute('node.add', [
      'node_type' => $bundle_type,
    ], $account);
  }
  elseif ($entity_type
    ->id() === 'taxonomy_term') {

    // Link to add a term of the specified type, then return to our view.
    $url = Url::fromRoute('entity.taxonomy_term.add_form', [
      'taxonomy_vocabulary' => $bundle_type,
    ], [
      'query' => $params,
    ]);
    $access = $this->accessManager
      ->checkNamedRoute('entity.taxonomy_term.add_form', [
      'taxonomy_vocabulary' => $bundle_type,
    ], $account);
  }
  else {

    // Try to get the entity creation path.
    $entity_links = $entity_type
      ->get('links');
    if (isset($entity_links['add-form'])) {

      // Replace the bundle token with the specified value.
      $path = preg_replace('/\\{[_a-z]+\\}/', $bundle_type, $entity_links['add-form']);
    }
    elseif (isset($entity_links['add-page'])) {
      $path = str_replace('{' . $entity_type
        ->id() . '}', $bundle_type, $entity_links['add-page']);
    }
    if (empty($path)) {

      // An entity we don't know how to process, so exit.
      // TODO: throw a warning?
      return;
    }

    // Prepend the path to make a valid internal URI.
    $path = 'internal:' . $path;
    $url = Url::fromUri($path, [
      'query' => $params,
    ]);

    // Now use the URL to check access.
    $route_name = $url
      ->getRouteName();
    $route_parameters = $url
      ->getrouteParameters();
    $access = $this->accessManager
      ->checkNamedRoute($route_name, $route_parameters, $account);
  }

  // Parse and sanitize provided classes.
  if ($this->options['class']) {
    $classes = explode(' ', $this->options['class']);
    foreach ($classes as $index => $class) {
      $classes[$index] = Html::getClass($class);
    }
  }
  else {
    $classes = [];
  }

  // Assemble elements into a link render array.
  $element = [
    '#type' => 'link',
    '#title' => $this->options['label'],
    '#url' => $url,
    '#options' => [
      'attributes' => [
        'class' => $classes,
      ],
    ],
    '#access' => $access,
  ];

  // Apply the selected dialog options.
  if ($this->options['target']) {
    $element['#options']['attributes']['class'][] = 'use-ajax';
    $width = $this->options['width'] ?: 600;
    $element['#options']['attributes']['data-dialog-options'] = Json::encode([
      'width' => $width,
    ]);
    switch ($this->options['target']) {
      case 'tray':
        $element['#options']['attributes']['data-dialog-renderer'] = 'off_canvas';
        $element['#options']['attributes']['data-dialog-type'] = 'dialog';
        break;
      case 'modal':
        $element['#options']['attributes']['data-dialog-type'] = 'modal';
        break;
    }
  }
  return $element;
}