Sunshine Admin Bundle
  • Sunshine Admin Bundle
  • Setup
  • Theme Configuration
  • YAML Entity configuration
  • YAML Entity List Sort
  • Menu Configuration
  • Field type and overrides
  • Entity Relations
  • Filtering : Default values
  • Filtering : Options multiple
  • Filtering : Custom values in Select2 lists
  • Roles & Permissions
  • Creating a page
  • Widgets
  • Generic Widgets : LIST
  • Tuto - 1/x - Create SF4 Project
Powered by GitBook
On this page

Filtering : Custom values in Select2 lists

PreviousFiltering : Options multipleNextRoles & Permissions

Last updated 6 years ago

Sometimes, it'll be very usefull to filter data in lists, for the current user, or simply set a custom list of values in a list.

You can configure a custom list of datas in each filters using a method in the repository avec YAML configuration.

Imagine the following model. We have a user, linked to a department (service) and to a company. We would like the filter to limit its values to the companies linked to the user.

The dropdown list we want to filter target the company (societe). It is in its repository that we'll add the custom method.

1/ Add method to the 'company' repository.

/** 
* Return all companies related to a user. 
* 
* @param array $identifier 
* @param string $toString
* @param $query
* @param array $callbackParams Parameters set in entity configuration yaml file
* @return QueryBuilder
*/
public function getUserAvailableSocietes( $identifier, $toString, $query, array $callbackParams = [])
{    
    $qb = $this->createQueryBuilder('s');    
    $qb->select(array('s.' . $identifier[0], 's.' . $toString . " AS text"));    
    if (!$this->user->hasRole('ROLE_ADMIN')) {        
        $qb ->innerJoin( 's.departments', 'se'  )            
            ->innerJoin( 'se.users', 'us' )            
            ->where( 'us.id = :userId' )            
            ->setParameter( 'userId', $this->user->getId() );    
    }    
    
    return $qb;
}

Note the signature of the method, you must respect it.

/**
* @param array $identifier 
* @param string $toString
* @param $query
* @param array $callbackParams Parameters set in entity configuration yaml file
* @return QueryBuilder
*/
public function getUserAvailableSocietes( $identifier, $toString, $query, array $callbackParams = [] )
  • $identifier : Is an array, where first index is the indentifier of the class. It should be used as 'value'.

  • $toString : Is the attribute you want to display as texte.

  • $query : Could be a prepared querybuilder, given to the method.

Be carrefull to select only 2 fields in your query :

  • The ID as first item.

  • The Text value as second.

If you do not respect that, you may have slow performances, and maybe errors returned from Symfony Serializer of circular references.

2/ Configure this custom data set in YAML

In the Entity YAML, configure your custom data set.tellaw_sunshine_admin:

  entities:    
    InfoRequest:      
      configuration:
        id: id
        class: App\Entity\InfoRequest
      attributes:
        id:
          label: Id
        question:
          label: Question          
          filterAttribute: label          
          relatedClass: App:Question        
        societe:          
          label: Société          
          filterAttribute: label          
          relatedClass: App:Societe          
          callbackFunction: getUserAvailableSocietes
          callbackParams: 
            param1: value1
            param2: value2                    ...

Note the usage of getUserAvailableSociete in the configuration. This will be used by the framework to be used as the callback when loading list datas.

callbackParams is an array of optional parameters that will be passed to the callback function getUserAvailableSociete.

This is it :-)

Filters at the top of a list.