简体   繁体   中英

TYPO3: Frontend Plugin Filter / Search

I've got a problem I can't find any documentation or solution for.

I've created a TYPO3 extension with a List and Detail View, everything works fine. Now I want to add some Input Fields above the List view, to let Site-Visitors Filter the List view.

How is this done? I'm sure I have to add a Fluid Form above the List View and handle this in Controller?

At the moment my Controller and Repo looks like this:

Controller:

/**
 * EventController
 */
class EventController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{

    /**
     * eventRepository
     * 
     * @var \Alroma\DsEventcalendar\Domain\Repository\EventRepository
     * @inject
     */
    protected $eventRepository = null;

    /**
     * action list
     * 
     * @return void
     */
    public function listAction()
    {   
        $events = $this->eventRepository->findAll();
        $this->view->assign('events', $events);
    }

    /**
     * action show
     * 
     * @param \Alroma\DsEventcalendar\Domain\Model\Event $event
     * @return void
     */
    public function showAction(\Alroma\DsEventcalendar\Domain\Model\Event $event)
    {
        $this->view->assign('event', $event);
    }

    /**
     * action frontpage
     * 
     * @return void
     */
    public function frontpageAction()
    {
        $events = $this->eventRepository->findAll();
        $this->view->assign('events', $events);
    }
}

Repo:

class EventRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{

    // Order by BE sorting
    protected $defaultOrderings = [
    'highlight' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING,
    'start' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
];
}

To be more specific: I want a User to have a Field, he can type some Keyword and I want to search it at my "Text" Database-Column of my Extension. If Keyword is found at any Text, show only the Article, that fits. Same for Date Parameter.

The most basic way is for server-side filtering:

  • create a form with an input for the query string to search for
  • Fetch the argument in your Controller
  • Filter your DB query by $query->like

Some code examples:

The form with an input for the argument "querystring"

<f:form action="list">
  <f:form.textfield name="querystring" value="" />
</f:form>

Get the argument in your action:

if ($this->request->hasArgument('querystring')) {
  $querystring = $this->request->getArgument('querystring');
  $events = $this->repository->findAll($querystring);
}

Filter your DB query in your repository (extend your findAll method or write a new method):

public function findAll($querystring = '') {
    $query = $this->createQuery();
    if ($querystring) {
        $query->matching(
            $query->like('property_to_search_in', '%'.$querystring.'%')
        );
    }
    return $query->execute();
}

If it's not too much content, I'd rather output search tags ( data- attributes) for all content and use javascript for filtering. Otherwise you'd need the page to reload just for displaying a subset of content already loaded for the client. Also it wouldn't feel smooth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM