public function ViewsAddButtonArea::render in Views Add Button 8
Same name and namespace in other branches
- 2.0.x src/Plugin/views/area/ViewsAddButtonArea.php \Drupal\views_add_button\Plugin\views\area\ViewsAddButtonArea::render()
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/ ViewsAddButtonArea.php, line 247
Class
- ViewsAddButtonArea
- Defines a views area plugin.
Namespace
Drupal\views_add_button\Plugin\views\areaCode
public function render($empty = FALSE) {
// 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']) : $this->options['button_classes'];
// Build custom attributes.
if ($this->options['button_attributes']) {
$attrs = $this->options['button_attributes'] ? $this
->tokenizeValue($this->options['button_attributes']) : $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();
}
// Get the url from the plugin and build the link.
if ($this->options['context']) {
$context = $this->options['tokenize'] ? $this
->tokenizeValue($this->options['context']) : $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) : $text;
$text = t($text);
// Generate the link.
$l = NULL;
/* @var $l \Drupal\Core\Link */
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) : $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) : $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' => '',
];
}
}
}