facet api: textbox filter - drupal

I'm looking to a way to add a textbox filter for searches of facet api in Drupal7. My site has three content type (A,B,C) that is used in searches.
I see that I can activate some filters in Admin -> configuration -> search and metadata -> Search API (edit one and then filters tab), but I can't see one of text. Is there a text field for filter in facet api?
Currently, I use Facet API and Drupal search, but the problem is that if I filter for content A and after I search something ("Andrew" for example) it resets the filter and search in all contents. I would like to filter and if I search something, search on the filtered content, not in a new search.
I have searched on Google, but I don't get a solution. The same problem is reported here, with a temporal solution, but I wnat to know if there is a better solution:
https://www.drupal.org/node/1381524
https://tech-tamer.com/drupal-7-refining-a-faceted-search-with-an-exposed-filter-solved/

Maybe You can deal with urls , reconstruct url from parameters to automatically check facets :
/**
* Implements hook_facetapi_searcher_info().
*
* #see hook_facetapi_searcher_info_alter()
*/
function MYMODULE_facetapi_searcher_info_alter(array &$searcher_info)
{
foreach ($searcher_info as $key => $value) {
if ($value['url processor'] == 'pretty_paths') {
$searcher_info[$key]['url processor'] = 'mymodule_urlprocessor';
}
}
}
/**
* Implements hook_facetapi_url_processors().
*
* #see hook_facetapi_url_processors()
*/
function MYMODULE_facetapi_url_processors()
{
return array(
'mymodule_urlprocessor' => array(
'handler' => array(
'label' => t('My URL processor'),
'class' => 'FacetapiUrlProcessorMyModule',
),
),
);
}
class FacetapiUrlProcessorMyModule extends FacetapiUrlProcessorPrettyPaths {
// overrides methods to construct url
}

Related

Sonata how make autocomplete filter on current entity property

I'm working on a sf2 project using Sonata Admin Bundle.
The project is a Donations website for humanitarian mission.
I have a 'Personne' entity, represent benefactors (donation makers).
My problem is the following:
I have to filter results in the sonata list view using autocompletion.
I want filter results using the 'name' property of the current entity ('Personne').
What I'm expecting :
$datagridMapper
->add('personne', 'doctrine_orm_model_autocomplete',
array('label' => 'AutoComplete'),
null,
array('property' => 'name'))
// error output : " The option `association_mapping` must be set for field: `personne` "
You can see my full admin class and entity on this gist :
https://gist.github.com/chalasr/0658a02b1c04180f5563
I understand this field type is reserved to entity associations (by example I already use it for filter results of my Donation entity by Personne name (other admin class).
My question is :
Is it possible to do what I need ?
If I can't do that using this field type, what is the right way to achieve this task ?
Thank's for your help.
After a lot of tests, it seems this functionality isn't yet provided by Sonata.
So, I had build an homemade autocomplete method in my admin controller and used it as ajax in my overridden CRUD:list.html.twig template.
This method takes field name, other autocomplete fields values, and keyword as parameters and reload results on keyup event.
You can look on this gist:
https://gist.github.com/chalasr/5c27ae64dc596967f18a
If you have an idea/proposition for optimize my code (simple autocomplete field type for $formMapper ?), I'm really interested.
Well it's possible to make a choice field and use integrated select2 to handle autocomplete, I don't know how well it would fare with large tables though.
$datagridMapper
->add('personne','doctrine_orm_callback', array(
'callback' => array($this, 'filterByName'),
'field_type' => 'text',
), 'entity',array(
'class' => 'AppBundle\Entity\Personne',
'choice_label' => 'name'
))
We add 'doctrine_orm_callback' because regular string filter can't handle EntityType Field, so we need to do it ourselves.
public function filterByName($queryBuilder, $alias, $field, $value)
{
if (!$value['value']) {
return;
}
$queryBuilder
->andWhere($alias . '.name' . ' = ' . ':name' )
->setParameter('name' , $value['value']->getName());
return true;
}
1 more thing, select2 won't create autocomplete (search) box if dropdown box has less than 10 choices, because it's set up in Admin.js that way.
select.select2({
width: function(){
// Select2 v3 and v4 BC. If window.Select2 is defined, then the v3 is installed.
// NEXT_MAJOR: Remove Select2 v3 support.
return Admin.get_select2_width(window.Select2 ? this.element : jQuery(this));
},
dropdownAutoWidth: true,
minimumResultsForSearch: 10,
allowClear: allowClearEnabled
});
so you need to override it, if you want that to be less.
Fonctionnality is now bundled with Sonata as simple as:
$filter->add('label', ModelFilter::class, ['field_type'=>ModelAutocompleteType::class,'field_options' => ['property' => 'name']])

datagrid filter for relation object as text field (insted of dropdown) in sonata admin in symfony 2.4

I have entity 'Action' with relation to 'User'. Created Admin CRUD controller in SonataAdminBundle. Everything works fine except user filter is rendered as dropdown list. I have 8k user count and growing so you must see why this is a problem.
I want user filter to be text input and on submit to search with LIKE %username%
Right now I add user filter like this - $datagridMapper->add('user').
I know I can add filter type and field type but I am not able to find the right combination and options. Found information on http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html but still no success.
Final solution
Following Alex Togo answer I used this code:
$datagridMapper->add('user', 'doctrine_orm_callback', array(
'callback' => function($queryBuilder, $alias, $field, $value) {
if (empty($value['value'])) {
return;
}
$queryBuilder->leftJoin(sprintf('%s.user', $alias), 'u');
$queryBuilder->where('u.username LIKE :username');
$queryBuilder->setParameter('username', '%'.$value['value'].'%');
return true;
},
'field_type' => 'text'
))
I needed something like this on a project. I implemented this feature using this. You can try to set the 'field_type' option to 'text' (I used 'choice' in the project I worked at) and add the querybuilder actions you need to filter.
Use doctrine_orm_choice option.
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add(
'module',
'doctrine_orm_choice',
[],
'choice',
[
'choices' => $this->filterModuleList
]
)
....

Drupal: Completely override search functionality but use Drupal's template system (from a module)

I'm writing a custom Drupal 7 module which will completely override the search page and search method for a website. Here is what I have so far:
/**
* Custom search.
*/
function mymodule_search_page() {
drupal_add_css('css/search.css');
// Perform a search (not important how)
$result = do_custom_search('foo');
return '<p>Results:</p>';
}
Now, as you can see, it's not complete. I don't know how to properly return structured HTML from this. How would I go about using Drupal's built-in template system to render the results?
you have to make use of drupal inbuilt functions. i hope you are looking for something like this http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_render/7
This is what I ended up doing:
/**
* Implements hook_menu().
*/
function mymodule_search_menu() {
$items = array();
$items['search'] = array('page callback' => 'mymodule_search_page',
'access callback' => TRUE);
return $items;
}
/**
* Mymodule search page callback.
*/
function mymodule_search_page() {
$variables = array();
// Add stuff to $variables. This is the "context" of the file,
// e.g. if you add "foo" => "bar", variable $foo will have value
// "bar".
...
// This works together with `mymodule_search_theme'.
return theme('mymodule_search_foo', $variables);
}
/**
* Idea stolen from: http://api.drupal.org/comment/26824#comment-26824
*
* This will use the template file custompage.tpl.php in the same
* directory as this file.
*/
function mymodule_search_theme() {
return array ('mymodule_search_foo' =>
array('template' => 'custompage',
'arguments' => array()));
}
Hope this helps someone!

Silverstripe tumblr-like Post Types

I am trying to create a back-end interface for silverstripe that gives the CMS user the option to choose between a set of Post Types (like tumblr) in Silverstripe3. So they can choose to create a News Post, Video Post, Gallery Post, etc.
I initially started off giving all Posts the necessary fields for each Type and adding an enum field that allowed the user to choose the Post Type. I then used the forTemplate method to set the template dependent upon which Post Type was chosen.
class Post extends DataObject {
static $db = array(
'Title' => 'Varchar(255),
'Entry' => 'HTMLText',
'Type' => 'enum('Video, Photo, Gallery, Music')
);
static $many_many = array(
'Videos' => 'SiteVideo',
'Photos' => 'SitePhoto,
'Songs' => 'SiteMp3'
);
public function forTemplate() {
switch ($this->Type) {
case 'Video':
return $this->renderWith('VideoPost');
break;
case 'Photo':
return $this->renderWith('ImagePost');
break;
etc...
}
function getCMSFields($params=null) {
$fields = parent::getCMSFields($params);
...
$videosField = new GridField(
'Videos',
'Videos',
$this->Videos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
$photosField = new GridField(
'Photos',
'Photos',
$this->Photos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
return $fields;
}
}
I would rather the user be able to choose the Post Type in the backend and only the appropriate tabs show up. So if you choose Video, only the Video GridField tab would show up. If you choose Photo Type only the Photo's GridField would show.Then I would like to be able to call something like
public function PostList() {
Posts::get()
}
and be able to output all PostTypes sorted by date.
Does anyone know how this might be accomplished? Thanks.
Well the first part can be accomplished using javascript. Check out this tutorial and the docs let me know if you have questions on it.
The second part would be trickier but I think you could do something with the page controller. Include a method that outputs a different template based on the enum value but you would have to set links somewhere.
I managed this with DataObjectManager in 2.4.7 as I had numerous DataObjects and all were included in one page but I'm not sure if that is feasible in SS3.
return $this->renderWith(array('CustomTemplate'));
This line of code will output the page using a different template. You need to include it in a method and then call that method when the appropriate link is clicked.

Sonata admin - "order by" field in related table

I have a Product admin class. The Product entity has a many-to-one relationship with a Category entity, i.e. a product is associated with a category.
In the admin "list" page for products, I need to sort by the category name (alphabetically) that each product is associated with.
Setting the default field to sort by is easy if the field is on the entity itself (see Sonata admin bundle order for how to do this). But I cannot figure out how to sort by a field in a related table.
Any help is appreciated.
It seems a workaround, but it works. You have to add a join overriding createQuery() method, than assign a default sortBy overriding $datagridValues:
<?php
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
class ExpenseAdmin extends Admin
{
protected $datagridValues = array(
'_page' => 1,
'_sort_order' => 'ASC', // sort direction
'_sort_by' => 'c.name' // field name
);
/**
* #return \Sonata\AdminBundle\Datagrid\ProxyQueryInterface
*/
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
return new ProxyQuery($query
->join(sprintf('%s.category', $query->getRootAlias()), 'c'));
}
}
Asume name is the property of entity Category by wich you want to sort. You may do this in you ProductAdmin.php
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('category.name', null, array(
'sortable' => true,
));
...
}
This way you leverage the ordering links in the header of the list, generated by Sonata.
Edit
If you would also like to have a link on the category name in products list to quickly edit the Category entity, assuming you have created a CategoryAdmin class, you should write your code like this:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('category', null, array(
'sortable' => 'category.name',
));
...
}
And in your Category class you should implement the __toString() method like this:
public function __toString()
{
return $this->getName();
}

Resources