2

QUESTION

I have an address field in my user form, from the address field module.

I have a content type named "project" which also have an address field and users can pick up a country related to the country the project will belong to.

I want to make a view filtering all the projects related to the logged-in user's address country field.

TRY

My trick is to make a user-view with a author-content relationship filtered on the content type project. Now I have the two fields in the view, I'm trying to compare them with a contextual php filter.

I tried to use php filter based on this post

$node = node_load($data->nid);
$user = user_load($user->uid);

$field_user_country = field_get_items('user', $user, 'field_user_address_country');
$country_user = field_view_value('user', $user, 'field_user_country', $field_user_country[0]);

$field_project_country = field_get_items('node', $node, 'field_project_range_country_country');
$project_country = field_view_value('node', $node, 'field_project_country', $$field_project_country[0]);

if ($price_a = $price_b) {
  return TRUE;
}

The fact is field_user_address_country is actually stored under field_data_field_user_address in the database.

Same for field_project_range_country_country which is stored under field_data_field_project_range_country

What is the best way to do this ? Is what I'm trying to do possible ?

UPDATE 1

Clear code which give me an ajax error :

$node = node_load($data->nid);
$user = user_load($user->uid);

$field_user_country = field_get_items('user', $user, 'field_user_address_country');
$country_user = field_view_value('user', $user, 'field_user_address_country', $field_user_country[0]);

$field_project_country = field_get_items('node', $node, 'field_project_range_country_country');
$project_country = field_view_value('node', $node, 'field_project_range_country_country', $field_project_country[0]);

if ($country_user == $project_country) {
  return TRUE;

UPDATE 2

Here the new code which is still giving me an ajax error :

EntityMalformedException : Missing bundle property on entity of type node

$node = node_load($data->nid);
$user = user_load($user->uid);

$field_user_country = field_get_items('user', $user, 'field_user_address_country');
$country_user = field_view_value('user', $user, 'field_user_address_country', $field_user_country[0]);

$field_project_country = field_get_items('node', $node, 'field_project_range_country_country');
$project_country = field_view_value('node', $node, 'field_project_range_country_country', $field_project_country[0]);

if ($country_user == $project_country) {
  return $country_user;
}

Have a bigger ajax error (like 15 lines) if I put

global $user 
$node = ($node = menu_get_object()) ? $node->nid : NULL;

At the beginning of my code.

9
  • if ($price_a = $price_b) { here you are setting $price_a equal to $price_b rather than checking if $price_a is equal to $price_b. You need to use == when performing checks. So this needs to be if ($price_a == $price_b) {
    – No Sssweat
    Feb 7, 2017 at 2:26
  • Yes surely. Was a newbie php mistake. It's still doesn't work.
    – Aporie
    Feb 8, 2017 at 2:18
  • That's because in your code $price_a and $price_b variables do not exist. try if ($country_user == $project_country) { also fix remove 1 $ from $$field_project_country[0]
    – No Sssweat
    Feb 8, 2017 at 2:23
  • Yeah yeah I've noticed that. But still, making a comparison between $country_user == $project_country give me an ajax error.
    – Aporie
    Feb 8, 2017 at 2:28
  • I aslo noticed that to use "field_view_value" function I should put the name of the field as 3rd argument and not the name of the variable previously defined. But still ajax error. In fact I presume that I can't get $data->nid and $user->uid from node_load and user_load. Trying to print them gave me an undefined variable error ....
    – Aporie
    Feb 8, 2017 at 2:35

1 Answer 1

1

Create a block view of Show: content, in contextual filter add Country.

Add this to your view contextual filter:

  global $user; // gets current logged in user object

  if (isset($user->field_user_address[LANGUAGE_NONE][0]['country'])) {      
    $user_country = $user->field_user_address[LANGUAGE_NONE][0]['country'];
    return $user_country;
  }
15
  • Rhaa, I'm not having an ajax error but still : Undefined property: stdClass::$field_project_range_country and Undefined property: stdClass::$field_user_address_country. Does variables gets values from the views fields (which are from the node) or from the node fields themselves ? If it's from the node maybe it's because the both fields are from an array from address_field ?
    – Aporie
    Feb 9, 2017 at 15:34
  • I tested this, I used the address field module. It gets them from nodes and user (not from views). Sounds like you have a different machine names for your fields? Check in Manage Display under the Machine Name column. Are you sure they are the same as my code? field_project_range_country and field_user_address_country
    – No Sssweat
    Feb 9, 2017 at 15:37
  • So, for the user page I have just one address field (which contain country, city and street references) with the machine name field_user_address. I picked up the field_user_address_country name from the mysql database. And for my content type "project" I have an address field which just contain country references with the machine name field_project_range_country. I don't know why we can't succed to have the field from the project page at least ...
    – Aporie
    Feb 9, 2017 at 15:53
  • In my code, adjust the user machine name and see what happens
    – No Sssweat
    Feb 9, 2017 at 16:04
  • When I did my test I had both with address, city, country, postal.
    – No Sssweat
    Feb 9, 2017 at 16:14

Your Answer

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

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