Hello.

I am trying to setup a default value according to drupal API (http://take.ms/sSbow) in case if the form is empty (before use filled any data) and not successful.
In hook_form_alter() I already added the code via $form['#info']['filter-field_geoaddress_proximity']['label'] = t('Distance'); and it works well. Hovewer if I try like these

$form['#info']['filter-field_geoaddress_proximity']['value']['#default_value'] = 21;
$form['field_geoaddress_proximity']['widget'][0]['value']['#default_value'] = 21;

nothing happens.

This my debug info:

Array
(
    [operator] => field_geoaddress_proximity_op
    [value] => 2ield_geoaddress_proximity
    [label] => Drupal\Core\StringTranslation\TranslatableMarkup Object
        (
            [string:protected] => Distance
            [translatedMarkup:protected] => 
            [options:protected] => Array
                (
                )

            [stringTranslation:protected] => 
            [arguments:protected] => Array
                (
                )

        )

    [description] => 
)

Could somebody help me? Doesn't matter it is in hook_form_alter() or any other hook.

P.S.: I use "Exposed form in block: Yes".

Thank you in advance.

Comments

Ruslan P created an issue. See original summary.

Ruslan Piskarov’s picture

Issue summary: View changes
Ruslan Piskarov’s picture

Issue summary: View changes
Ruslan Piskarov’s picture

Issue summary: View changes
mikeker’s picture

Status: Active » Postponed (maintainer needs more info)

Usually default values are set by selecting one (or more) options in the "Configure filter criterion" dialog. See my reply to your Drupal Answers post.

If that fixes the issue, feel free to close this issue. Otherwise I'll leave it open for a month for replies.

Ruslan Piskarov’s picture

Thank you @mikeker.

I will try in coming days.
Actually in my case. it isn't fixed value, a lot of PHP logic. So I am interested how to update values in block (form) programmatically using "Better Exposed Filters".

mikeker’s picture

@Ruslan P, you'll need to implement hook_views_pre_execute if you have to use logic to come up with your default values. You can change the default value in the form via hook_form_alter but that won't change the values in the results until the form is submitted.

Ruslan Piskarov’s picture

Hello @mikeker,
thank you for the answer.

Actually, I tried use hook_views_pre_execute like the following code and nothing heppens.

/**
 * Implements hook_views_pre_execute().
 */
function userlocation_views_pre_execute(ViewExecutable $view) {
  if ($view->id() == 'searchgeo_v2') {
    $view_filters = $view->display_handler->getOption('filters');
    $view_filters['field_geoaddress_proximity']['value'] = 21;
//    $view->display_handler->setOption('filters', $view_filters);
    $view->display_handler->overrideOption('filters', $view_filters);
  }
}

Also the following code works for me, but only in case with "Exposed form in block: No".
Or in another word:
if I setup Exposed form in block: No", works anything.
if I setup Exposed form in block: Yes", works only view's result, however then fields in the block are empty.

/**
 * Implements hook_views_pre_build().
 *
 * @param \Drupal\views\ViewExecutable $view
 */
function userlocation_views_pre_build(ViewExecutable $view) {

  if ($view->id() == 'searchgeo_v2'
    || $view->id() == 'duplicate_of_teachers_near_me'
    || $view->id() == 'teachers_near_me'
  ) {
    $default_exposed_filters = [];
    $exposed_filters = $view->getExposedInput();

    if (!isset($exposed_filters['field_geoaddress_proximity'])) {
      $default_exposed_filters['field_geoaddress_proximity'] = 15;
    }

    if (!isset($exposed_filters['field_geoaddress_proximity-lat'])
      && !isset($exposed_filters['field_geoaddress_proximity-lng'])
    ) {
      if (\Drupal::moduleHandler()->moduleExists('device_geolocation')) {
        $session = \Drupal::service('session');
        $smart_ip_session = $session->get('smart_ip')['location'];
        if (isset($smart_ip_session['latitude']) && isset($smart_ip_session['longitude'])) {
          $address = '';
          if (isset($smart_ip_session['street_number'])) {
            $address .= ', ' . $smart_ip_session['street_number'];
          }
          if (isset($smart_ip_session['route'])) {
            $address .= ', ' . $smart_ip_session['route'];
          }
          if (isset($smart_ip_session['locality'])) {
            $address .= ', ' . $smart_ip_session['locality'];
          }
          if (isset($smart_ip_session['country'])) {
            $address .= ', ' . $smart_ip_session['country'];
          }
          if (isset($smart_ip_session['countryCode'])) {
            $address .= ', ' . $smart_ip_session['countryCode'];
          }
          $address = trim(trim($address, ','));
          $default_exposed_filters['geolocation_geocoder_google_geocoding_api'] = $address;
          $default_exposed_filters['field_geoaddress_proximity-lat'] = $smart_ip_session['latitude'];
          $default_exposed_filters['field_geoaddress_proximity-lng'] = $smart_ip_session['longitude'];
        }
      }
    }

    if (!empty($default_exposed_filters)) {
      $view->setExposedInput($default_exposed_filters);
    }
  }
}

Sccreensnap: http://take.ms/Wuf57.

mikeker’s picture

Interesting that it works as a regular exposed form, but not as an exposed block...

Have you tried this without BEF? Do you get the same results? If so, I suspect this is a core Views issue and you'll likely get faster (and more knowledgeable!) replies from the core issue queue.

Sorry, I don't have the time at the moment to investigate this any further.

Ruslan Piskarov’s picture

Hello @mikeker,

> Have you tried this without BEF?

No. I will. A good idea. Thank you. Will leave a comment after the investigation.

> I don't have the time at the moment to investigate this any further.

Don't worry about it ))

Ruslan Piskarov’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

@mikeker,

I am confirming. The same result then I use "Exposed form style:Basic". So I will investigate more and find/create drupal core issue.

Thank you very much.

mikeker’s picture

@Ruslan P, please post any core issues you find here as I'd like to track them. Thanks!