You are here

public function ViewsAddButtonField::render in Views Add Button 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/views/field/ViewsAddButtonField.php \Drupal\views_add_button\Plugin\views\field\ViewsAddButtonField::render()

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

src/Plugin/views/field/ViewsAddButtonField.php, line 221

Class

ViewsAddButtonField
Defines a views field plugin.

Namespace

Drupal\views_add_button\Plugin\views\field

Code

public function render(ResultRow $values) {

  // Get the entity/bundle type.
  $type = explode('+', $this->options['type'], 2);

  // If we do not have a '+', then assume we have a no-bundle entity type.
  $entity_type = $type[0];
  $bundle = isset($type[1]) ? $type[1] : $type[0];

  // Load ViewsAddButton plugin definitions, and find the right one.
  $plugin_manager = \Drupal::service('plugin.manager.views_add_button');
  $plugin_definitions = $plugin_manager
    ->getDefinitions();
  $plugin_class = $plugin_definitions['views_add_button_default']['class'];
  if (isset($this->options['render_plugin']) && !empty($this->options['render_plugin'])) {
    $plugin_class = $plugin_definitions[$this->options['render_plugin']]['class'];
  }
  else {
    $set_for_bundle = FALSE;
    foreach ($plugin_definitions as $pd) {

      // Exclude 'manual selection' special-use plugins.
      if (empty($pd['manual_select']) || !$pd['manual_select']) {
        if (!empty($pd['target_entity']) && $pd['target_entity'] === $entity_type) {
          if (!empty($pd['target_bundle'])) {
            $b = $bundle;

            /*
             * In certain cases, like the Group module,
             * we need to extract the true bundle name from a
             * hashed bundle string.
             */
            if (method_exists($pd['class'], 'get_bundle')) {
              $b = $pd['class']::get_bundle($bundle);
            }
            if ($pd['target_bundle'] === $b) {
              $plugin_class = $pd['class'];
              $set_for_bundle = TRUE;
            }
          }
          elseif (!$set_for_bundle) {
            $plugin_class = $pd['class'];
          }
        }
      }
    }
  }
  if ($this
    ->checkButtonAccess($plugin_definitions, $plugin_class, $entity_type, $bundle)) {

    // Build URL Options.
    $opts = [];
    if ($this->options['destination']) {
      $dest = Url::fromRoute('<current>');
      $opts['query']['destination'] = $dest
        ->toString();
    }
    $opts['attributes']['class'] = $this->options['tokenize'] ? $this
      ->tokenizeValue($this->options['button_classes'], $values->index) : $this->options['button_classes'];

    // Build custom attributes.
    if ($this->options['button_attributes']) {
      $attrs = $this->options['button_attributes'] ? $this
        ->tokenizeValue($this->options['button_attributes'], $values->index) : $this->options['button_attributes'];
      $attr_lines = preg_split('/$\\R?^/m', $attrs);
      foreach ($attr_lines as $line) {
        $attr = explode('=', $line);
        if (count($attr) === 2) {
          $opts['attributes'][$attr[0]] = $attr[1];
        }
      }
    }

    // Build query string.
    if ($this->options['query_string']) {
      $opts['query'] = $this
        ->getQueryString($values);
    }

    // Get the url from the plugin and build the link.
    if ($this->options['context']) {
      $context = $this->options['tokenize'] ? $this
        ->tokenizeValue($this->options['context'], $values->index) : $this->options['context'];
      $url = $plugin_class::generateUrl($entity_type, $bundle, $opts, $context);
    }
    else {
      $url = $plugin_class::generateUrl($entity_type, $bundle, $opts);
    }
    $text = $this->options['button_text'] ? $this->options['button_text'] : 'Add ' . $bundle;
    $text = $this->options['tokenize'] ? $this
      ->tokenizeValue($text, $values->index) : $text;

    // Generate the link.
    $l = NULL;
    if (method_exists($plugin_class, 'generateLink')) {
      $l = $plugin_class::generateLink($text, $url, $this->options);
    }
    else {
      $l = ViewsAddButtonDefault::generateLink($text, $url, $this->options);
    }
    $l = $l
      ->toRenderable();

    // Add the prefix and suffix.
    if (isset($this->options['button_prefix']) || isset($this->options['button_suffix'])) {
      if (!empty($this->options['button_prefix']['value'])) {
        $prefix = check_markup($this->options['button_prefix']['value'], $this->options['button_prefix']['format']);
        $prefix = $this->options['tokenize'] ? $this
          ->tokenizeValue($prefix, $values->index) : $prefix;
        $l['#prefix'] = $prefix;
      }
      if (!empty($this->options['button_suffix']['value'])) {
        $suffix = check_markup($this->options['button_suffix']['value'], $this->options['button_suffix']['format']);
        $suffix = $this->options['tokenize'] ? $this
          ->tokenizeValue($suffix, $values->index) : $suffix;
        $l['#suffix'] = $suffix;
      }
      return $l;
    }
    return $l;
  }
  else {
    if (isset($this->options['button_access_denied']['value']) && !empty($this->options['button_access_denied']['value'])) {
      $markup = check_markup($this->options['button_access_denied']['value'], $this->options['button_access_denied']['format']);
      $markup = $this->options['tokenize'] ? $this
        ->tokenizeValue($markup) : $markup;
      return [
        '#markup' => $markup,
      ];
    }
    else {
      return [
        '#markup' => '',
      ];
    }
  }
}