1

I followed this link to transform my exposed filter from textbox to dropdown list.

Use dropdown in views exposed filter on text fields with entity not node?

I make it work with a custom textfield.

I have problem to use the module with node title.

My code till now is:

<?php

function views_dropdown_form_views_exposed_form_alter(&$form, &$form_state) {
global $user;

// Only alter forms with the necessary field
if (isset($form['title'])) {

// Build a query to get all node ids having the specified field
$query = new EntityFieldQuery();

$results = $query->entityCondition('entity_type', 'node')
                  ->entityCondition('bundle', 'rapporto_di_lavoro')
                  ->propertyCondition('uid', $user->uid)
                  ->propertyCondition('title', 'NULL', '!=')
                  ->execute();


// Attach the field values to the nodes
$ids = array_keys($results['node']);
print_r($ids);
print('<br>');
// Add a default so the filter is optional
$options = array('' => '<select>');

// Buld the options array based on the query results, overwriting duplicate entries
$query = db_select('node', 'n')
            ->fields('n', array('title'))     
            ->condition('n.nid', $ids, 'IN');
$options = $query->execute()->fetchAll();

$op = json_decode(json_encode($options), true);
print_r($op);
print('<br>');


// Attach the field values to the nodes
//$nodes = $results['node'];
//field_attach_load('node', $nodes, FIELD_LOAD_CURRENT, array('field_id' => $field_id));
// Buld the options array based on the query results, overwriting duplicate entries
//foreach($nodes as $nid => $node) {
  //$value = $node->field_cognome_l['und'][0]['value'];
  //$options2[$value] = $value; }



//Alter the field
$form['title']['#type'] = 'select';
$form['title']['#options'] = $op;
$form['title']['#size'] = 1;
}
}

I feel close to solution but I miss something.

On the picture I print four arrays.

enter image description here

Only the 4th was working when I tested the module with a custom field,

so probably I need to transform the 3rd array into the 4th.

3

I solved the problem, here the code:

<?php

function views_dropdown_form_views_exposed_form_alter(&$form, &$form_state) {
global $user;

// Only alter forms with the necessary field
if (isset($form['title'])) {

// Build a query to get all node ids having the specified field
$query = new EntityFieldQuery();

$results = $query->entityCondition('entity_type', 'node')
                  ->entityCondition('bundle', 'rapporto_di_lavoro')
                  ->propertyCondition('uid', $user->uid)
                  ->propertyCondition('title', 'NULL', '!=')
                  ->execute();


// Attach the field values to the nodes
$ids = array_keys($results['node']);
// Add a default so the filter is optional
$options = array('' => '<select>');

// // Buld the options array based on the query results, overwriting duplicate entries
$query = db_select('node', 'n')
            ->fields('n', array('title'))     
            ->condition('n.nid', $ids, 'IN');

$results = $query->execute()->fetchAll();

foreach($results as $id => $node) {
  $value = $node->title;
  $options[$value] = $value;
}


// // Alter the field
$form['title']['#type'] = 'select';
$form['title']['#options'] = $options;
$form['title']['#size'] = 1;
}}

enter image description here

I hope could help someone :)

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.