0

I have a content type which have a name field and address field.

I want to retrieve programmatically all the nodes in a specific city and wich contain in his name one or many keywords.

For example i want to search in "Paris" the node wich contain in name the word "beautiful" and/or "shop". Then i can retrieve theses results : - "My beautiful shop of candies" in "Paris" - "Fast shop" in "Paris"

The problem is that i don't know how to get nodes by a word containing in :/

Here is my current code :

$query  =  new EntityFieldQuery();

$entities  =  $query->entityCondition( 'entity_type', 'node' )
    ->propertyCondition( 'status', 1)
    ->fieldCondition( 'field_adress', 'locality', 'Paris', 'like' )
    ->fieldCondition( 'field_name', 'value', array('beautiful', 'shop', 'money') )
    ->execute();

dsm($entities);

I tried to change :

->fieldCondition( 'field_name', 'value', array('beautiful', 'shop', 'money') )

to

->fieldCondition( 'field_name', 'value', array('beautiful', 'shop', 'money'), 'CONTAINS' )

but I get this error :

Warning : addcslashes() expects parameter 1 to be string, array given dans DatabaseConnection->escapeLike() (ligne 984 dans /Applications/MAMP/htdocs/fritr/includes/database/database.inc).

1

You could do this by building the query up in smaller chunks and using a loop to add the field conditions for the mulitple words . I'm not sure how of how you'll be passing the data in but say you have an array of words in $words.

$query = new EntityFieldQuery();
$query-->entityCondition( 'entity_type', 'node' )
    ->propertyCondition( 'status', 1)
    ->fieldCondition( 'field_adresse', 'locality', 'Paris' );

foreach ($words as $word){
   $query->fieldCondition('name', 'value', $word, 'CONTAINS');
 }

$result = $query->execute();

This code isn't tested and I've written off the top of my head. https://api.drupal.org/api/drupal/includes!entity.inc/function/EntityFieldQuery%3A%3AfieldCondition/7.x

  • Ahh it's an another approach i try it and tell you if it works – Kpone Oct 20 '16 at 12:53

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.