0

I have a custom content entity 'service' created with Drupal Console (drupal generate:entity:content). I added my own fields to the standard ones created by DC. Of which 'additiondate':

$fields['additiondate'] = BaseFieldDefinition::create('datetime')
  ->setLabel(t('Addition Date'))
  ->setSetting('datetime_type', 'date')
  ->setDefaultValue(array(0 => array(
    'default_date_type' => 'now',
    'default_date' => 'now',
  )))
  ->setReadOnly(TRUE);

Doing this way stores the date in YYYY-MM-DD string format.
I created an Embed View on this custom content entity to be used with views_embed_view().
I'd like to use Contextual Filter on the field 'additiondate' in order to have all the services that have been added SINCE the date passed as parameter.
The only way to pass a parameter seems to be '2018-04-26' and the result is all the services that have been added ON this date, as you can see in the Preview of my View with the generated request displayed: enter image description here Any idea?

Here is the screen capture of my options when I add the contextual filter:
enter image description here

  • Does "greater than or equal to" show up in the options when you configure the filter? – mpdonadio May 13 '18 at 17:36
  • No. But as it is a Contextual Filter, I assume it is normal. The options I have are about 1. what to do when the filter value is not available, 2. what to do when a filter value is available or a default value has been provided... (I updated my question with a screen capture). – gbmapo May 13 '18 at 18:27
0

Finally, I made it work.
I used mymodule_views_query_alter replacing the operator of the proper condition:

  if ($view->id() == 'services' && $view->current_display == 'embed_2') {
    foreach ($query->where as &$condition_group) {
      foreach ($condition_group['conditions'] as &$condition) {
        if ($condition['field'] == 'service.additiondate') {
          $condition['operator'] ='>=';
        }
      }
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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