Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCC-83: Allows to configure link in tags. #13

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 129 additions & 7 deletions src/Plugin/views/field/PccTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,147 @@

namespace Drupal\pcx_connect\Plugin\views\field;

use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\PrerenderList;
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
use Drupal\views\ResultRow;

/**
* Handler to render html markup.
* Field handler to provide a list of permissions.
*
* @ingroup views_field_handlers
*
* @ViewsField("pcc_tags")
*/
class PccTags extends FieldPluginBase {
class PccTags extends PrerenderList {
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
$tags = (!empty($value)) ? implode(", ", $value) : '';
return $tags;
protected function defineOptions() {
$options = parent::defineOptions();

$options['tags_make_link'] = ['default' => FALSE];
$options['tags_path'] = ['default' => ''];

return $options;
}

/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['tags_make_link'] = [
'#type' => 'checkbox',
'#title' => $this->t('Output tags as a custom link'),
'#default_value' => $this->options['tags_make_link'],
];
$form['tags_path'] = [
'#title' => $this->t('Tags link path'),
'#type' => 'textfield',
'#default_value' => $this->options['tags_path'],
'#description' => $this->t('The Drupal path or absolute URL for this link. The URL should start with /. You may enter {{ tag }} as a replacement pattern.'),
'#states' => [
'visible' => [
':input[name="options[tags_make_link]"]' => ['checked' => TRUE],
],
],
'#maxlength' => 255,
];
unset($form['alter']['make_link']);
unset($form['alter']['path']);
}

/**
* {@inheritdoc}
*/
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
if (!str_starts_with($form_state->getValue(['options', 'tags_path']), '/')) {
$form_state->setError($form['tags_path'], $this->t('The Drupal path or absolute URL for this link should start with /.'));
}
}

/**
* {@inheritdoc}
*/
public function preRender(&$values) {
$this->items = [];
foreach ($values as $row => $result) {
$tags = $this->getValue($result);
if (!empty($tags)) {
$this->items[$row]['tags'] = $tags;
}
}
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

/**
* {@inheritdoc}
*/
public function render_item($count, $item) {
return $item['tags'];
}

/**
* {@inheritdoc}
*/
public function getItems(ResultRow $values) {
$field = $this->getValue($values);
if (!$field) {
return [];
}
$items = [];
foreach ($this->items as $row => $item) {
if (!array_diff($field, $item['tags'])) {
foreach ($item['tags'] as $key => $tag) {
$items[$row][$key]['tags'] = $tag;
}
return $items[$row];
}
}
return $items;
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

/**
* {@inheritdoc}
*/
public function renderText($alter) {
$value = (string) $this->last_render;
if ($this->options['tags_make_link']) {
$tags_path = $this->options['tags_path'];
$tag_link = $this->renderTagAsLink($tags_path, $value);
return Markup::create($tag_link);
}
return $value;
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

/**
* Render tag as a link.
*
* @param string $tags_path
* Tags custom path.
* @param string $value
* The tag value.
*
* @return string
* Returns the tag link.
*/
private function renderTagAsLink($tags_path, $value): string {
$token = '{{ tag }}';
if ($tags_path != '<front>') {
$tags_path = strip_tags($tags_path);
$link_value = str_replace(' ', '-', strtolower($value));
$link_path = str_replace($token, $link_value, $tags_path);
$tags_url = Url::fromUserInput($link_path);
$render = [
'#type' => 'link',
'#title' => $value,
'#url' => $tags_url,
];
return $this->getRenderer()->render($render);
}
return $value;
}

}