I am trying to create a simple time tracking app and for this purpose, I need an entity reference field with some additional filtering of referenced entities (only active projects can be selected).

I read some guides and it seems that what I need is to

1. create entity reference field

This is done in the buildForm() function of the src/Form/TimesheetForm.php class like this:

    $form['project'] = [
  '#type' => 'entity_autocomplete',
  '#title' => $this->t('Project'),
  '#weight' => '0',
  '#target_type' => 'project_entity',
  '#selection_handler' => 'default:active_projects',
  '#selection_settings' => [

  '#tags' => FALSE,

2. create custom plugin

This is done in src/Plugin/ActiveProjectsSelection.php like this:


    namespace Drupal\timesheet\Plugin\EntityReferenceSelection;

use Drupal\node\Plugin\EntityReferenceSelection\NodeSelection;

 * Provides specific access control for the node entity type.
 * @EntityReferenceSelection(
 *   id = "active_projects",
 *   label = @Translation("Active projects selection"),
 *   entity_types = {"project_entity"},
 *   group = "default",
 *   weight = 3
 * )

class ActiveProjectsSelection extends NodeSelection {

   * @param null $match
   * @param string $match_operator
   * @return \Drupal\Core\Entity\Query\QueryInterface
  protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
    $query = parent::buildEntityQuery($match, $match_operator);
    //$query->condition('project_start_date', time(), '<');
    //$query->condition('project_end_date', time(), '>');
    return $query;


However, even when I commented out all the logic, the autocomplete does not work. When I type in the field, no autocomplete popup appears. Not even the debug message is logged when I try to trigger the autocomplete.

I have also tried to extend DefaultSelection instead of the NodeSelection as I am using a custom entity which is not a node, but the result was the same.

It looks like my plugin is not even reached, but I do not know why.

I cleared the cache, my local site is in dev mode.

  • 1
    The file's in the wrong folder - put it in a subfolder called EntityReferenceSelection and clear the cache again, should be picked up
    – Clive
    Dec 10, 2018 at 0:00

1 Answer 1


So, with help from Clive (see his comment below the original question) I have identified two issues:

  1. The correct path for the ActiveProjectsSelection.php file should be src/Plugin/EntityReferenceSelection/ActiveProjectsSelection.php.

  2. The id of the plugin in the annotation should be default:active_projects.

  3. (not related to the original problem) The correct timestamp for the date comparison is DrupalDateTime::createFromTimestamp(time()).

With these changes, my code works perfectly. Also, it does not matter whether I extend NodeSelection or DefaultSelection, it works with either of them.

  • 1
    The plugin ID also needs to meet certain parameters, but that is largely undocumented. It looks like you were able to hit on a pattern that works! See the core issue at drupal.org/project/drupal/issues/2649712
    – akalata
    May 10, 2019 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.