Skip to content

Commit

Permalink
favorites #827
Browse files Browse the repository at this point in the history
search api: store favorites - block: create favorites block to show facet for current user
  • Loading branch information
protitude committed Feb 3, 2025
1 parent 3f158f8 commit bcc8493
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src/Plugin/Block/OdaFavorites.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Drupal\oda\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* ODA Reports favorites Block.
*
* @Block(
* id = "oda_favorites",
* admin_label = @Translation("ODA Favorites"),
* )
*/
class OdaFavorites extends BlockBase implements
ContainerFactoryPluginInterface {

/**
* Current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;

/**
* {@inheritdoc}
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* Container pulled in.
* @param array $configuration
* Configuration added.
* @param string $plugin_id
* Plugin_id added.
* @param mixed $plugin_definition
* Plugin_definition added.
*
* @return static
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_user')
);
}

/**
* {@inheritdoc}
*
* @param array $configuration
* Configuration array.
* @param string $plugin_id
* Plugin id string.
* @param mixed $plugin_definition
* Plugin Definition mixed.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* Current user.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $current_user;
}

/**
* {@inheritdoc}
*/
public function build() {
$uid = $this->currentUser->id();

// Create link '/reports?f%5B0%5D=oda_favorite_data_reports%3A' . $uid.
$link = '/reports?f%5B0%5D=oda_favorite_data_reports%3A' . $uid;

return [
'#markup' => '<a href="' . $link . '">Favorites</a>',
];
}

/**
* Set cache tag by user.
*/
public function getCacheTags() {
if ($user = $this->currentUser) {
return Cache::mergeTags(parent::getCacheTags(), ['user:' . $user->id()]);
}
else {
return parent::getCacheTags();
}

}

/**
* Return cache contexts.
*/
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), ['user']);
}

}
67 changes: 67 additions & 0 deletions src/Plugin/search_api/processor/DataFavorites.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Drupal\oda\Plugin\search_api\processor;

use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Item\ItemInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Processor\ProcessorProperty;

/**
* Search API Processor for indexing data reports favorites.
*
* @SearchApiProcessor(
* id = "custom_data_favorites",
* label = @Translation("Custom Event Type"),
* description = @Translation("Add event type to the index."),
* stages = {
* "add_properties" = 0,
* },
* locked = true,
* hidden = true,
* )
*/
class DataFavorites extends ProcessorPluginBase {

/**
* {@inheritdoc}
*/
public function getPropertyDefinitions(?DatasourceInterface $datasource = NULL) {
$properties = [];

if (!$datasource) {
$definition = [
'label' => $this->t('Custom Favorite data reports'),
'description' => $this->t('Data reports favorites.'),
'type' => 'string',
'processor_id' => $this->getPluginId(),
];
$properties['search_api_custom_data_report_fav'] = new ProcessorProperty($definition);

}
return $properties;
}

/**
* {@inheritdoc}
*/
public function addFieldValues(ItemInterface $item) {
$entity = $item->getOriginalObject()->getValue();

$flag = \Drupal::service('flag')->getFlaggingUsers($entity);

$flag_uids = array_keys($flag);

$fields = $item->getFields();
$fields = $this->getFieldsHelper()
->filterForPropertyPath($fields, NULL, 'search_api_custom_data_report_fav');
foreach ($fields as $field) {

foreach ($flag_uids as $uid) {
$field->addValue($uid);
}

}
}

}

0 comments on commit bcc8493

Please sign in to comment.