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

[FEATURE] adds HighlightAuthorViewHelper to highlight an Author ()by … #30

Merged
merged 17 commits into from
Jan 10, 2022
Merged
Changes from 2 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
86 changes: 86 additions & 0 deletions Classes/ViewHelpers/Format/HighlightAuthorViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace In2code\Publications\ViewHelpers\Format;


use TYPO3\CMS\Core\Utility\GeneralUtility;
use In2code\Publications\Domain\Model\Author;
use In2code\Publications\Domain\Repository\AuthorRepository;
use In2code\Publications\Utility\ObjectUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
* Class HighlightAuthorViewHelper
* wraps html around search terms
*/
class HighlightAuthorViewHelper extends AbstractViewHelper
{

/**
* @var bool
*/
protected $escapeOutput = false;

/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('author', 'mixed', 'Author array', true);
$this->registerArgument('before', 'string', 'Add this html before Author of interest', false);
$this->registerArgument('after', 'string', 'Add this html after', false);
$this->registerArgument('searchterms','mixed', 'Searchterms, Authors selected in Filter in flexform/TS ', false);
}

/**
* @return string
*/
public function render(): string
{
$value = implode(PHP_EOL, GeneralUtility::trimExplode(PHP_EOL, $this->renderChildren(), true));
if (!empty($value)) {
if (!empty($this->arguments['searchterms'])) {
$filterauthors=$this->getAuthors($this->arguments['searchterms']);
$author[]= $this->arguments['author'];
karliwalti marked this conversation as resolved.
Show resolved Hide resolved
$match = array_intersect($filterauthors,$author); //intersect_assoc is too strict
if(!empty($match)) {
return $this->wrapText($value);
}
}
}
return $value;
}

/**
* @param string $text
* @return string
*/
protected function wrapText(string $text): string
{
if (!empty($this->arguments['before'])) {
$text = $this->arguments['before'] . $text;
}
if (!empty($this->arguments['after'])) {
$text .= $this->arguments['after'];
}
return $text;
}


/**
* @param string $filter
* @return array
*/
protected function getAuthors(string $filter): array
{
$authors = [];
$authorRepository = ObjectUtility::getObjectManager()->get(AuthorRepository::class);
foreach (GeneralUtility::intExplode(',', $filter, true) as $identifier) {
$authors[] = $authorRepository->findByUid($identifier);
}
// }
karliwalti marked this conversation as resolved.
Show resolved Hide resolved
return $authors;
}

karliwalti marked this conversation as resolved.
Show resolved Hide resolved
}