Skip to content

Commit

Permalink
Merge pull request #45 from rosiel/search-formatter
Browse files Browse the repository at this point in the history
Add Searchable Entity Formatter.
  • Loading branch information
Natkeeran authored May 22, 2024
2 parents a63808a + 683c40f commit 5186018
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/Plugin/Field/FieldFormatter/SearchableEntityFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Drupal\advanced_search\Plugin\Field\FieldFormatter;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter;
use Drupal\Core\Form\FormStateInterface;

/**
* Plugin implementation of the 'searchable_entity_formatter' formatter.
*
* @FieldFormatter(
* id = "searchable_entity_formatter",
* label = @Translation("Searchable entity formatter"),
* field_types = {
* "entity_reference"
* }
* )
*/
class SearchableEntityFormatter extends EntityReferenceLabelFormatter {

/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'search_link' => 'search?f[0]',
'search_var' => 'all_subjects',
'search_term' => FALSE,
] + parent::defaultSettings();
}

/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['search_link'] = [
'#title' => t('Search base path'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => $this->getSetting('search_link'),
];
$elements['search_var'] = [
'#title' => t('Search variable'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => $this->getSetting('search_var'),
];
$elements['search_term'] = [
'#title' => t('Use label as search term'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('search_term'),
];
return $elements;
}

/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = t('Search path: @search_link', ['@search_link' => $this->getSetting('search_link')]);
$summary[] = t('Variable: @search_var', ['@search_var' => $this->getSetting('search_var')]);
$summary[] = $this->getSetting('search_term') ? t('Use label as search term') : t('Use ID as search term');
return $summary;
}

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$par_elements = parent::viewElements($items, $langcode);
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
$search_var = $this->getSetting('search_var');
$search_term = $this->getSetting('search_term');

if ($search_term == TRUE) {
$param = $par_elements[$delta]['#title'];
}
else {
$param = $entity->id();
}

$url = \Drupal::service('facets.utility.url_generator')->getUrl([$search_var => [$param]]);

$par_elements[$delta]['#url'] = $url;

}
return $par_elements;
}

/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity) {
return $entity->access('view label', NULL, TRUE);
}

}

0 comments on commit 5186018

Please sign in to comment.