It looks like other people have reported this before and almost looks like it's been fixed but with dev version and search api this doesn't seem to work as expected.
In the fields settings tab of search api I only get

Country of residence » ISO2 country code

Comments

alan d.’s picture

It looks like you were testing 2 hours prior to #1609150: Support entity label (and callback) being pushed through. Without testing this, I think this will allow the Entity API understand the country entities label (aka, the country name) and use this when relevant, that should then show up in the Search API too. Well at least that's what should happen!

Can you let me know if this seemingly unrelated issue has resolved this? Thanks

alan d.’s picture

Scrape that; you were testing in July 29, that fix was committed June 29.

Andrew Edwards’s picture

Hello. I too would like to have country name exposed to Search API. At the moment, like Alan D, I see only 'Country » ISO2 country code'. I'm using 7.x-2.0+10-dev

Cheers!

Andrew

Anonymous’s picture

Looking for the same thing.

I found that making a change in countries_field_info_property_callback() can expose all country fields.
Although there is a warning not to do it, if you comment out line 38, that changes $property['type'], the entity will be used to set which fields will be available, compared to just the iso2 field that is defined.

I realize the comment is there, but what was the reasoning for saying do not use the entity type? Are there issues that crop up elsewhere because of that? And if so, could more fields be added to the $property['property info'] array to expose more fields manually?

Andrew Edwards’s picture

I'm wondering if the issue has to do with iso2 (as opposed to cid) being used as the primary key in the table countries_country.
I used cid (wrongly I now realise) as a foreign key in a custom entity. As a result I can see all country fields when I index on my custom entity using search api.

Perhaps Search API does not recognise the 'name' key in hook_entity_info()?

Andrew Edwards’s picture

I've created a feature request in the search api issue queue to try and get the name key on hook_entity_info supported #1998924: Why does the Countries module not work with the Search API?.

Also see #1142536: How to integrate the countries module with the entity api for more details.

Andrew Edwards’s picture

Category: bug » feature

After discussion with the module maintainer for Search API it looks like the problem isn't at that end.
#1998924: Why does the Countries module not work with the Search API?

As r_smylski pointed out it looks like the problem is with countries_field_info_property_callback(). I think that as a work around 'countries' has been used for $property['type'] instead of 'country' (the entity type). As a result when Search API tries to grab the properties for the country entity type it can't because it's looking for countries.

Could it be that the way the entity type and bundle for country is set up needs to be changed? It looks as though 'countries' has been used for $property['type'] because 'country' is used for both the entity type and the bundle ... and the code author didn't want to reference the entity type. All a bit confusing!

I tried changing property['type'] to 'country' and that works to an extent. Search API then picks up the properties. Unfortunately though the values for the properties were not indexed.

I'm going to change this issue to a bug report as it looks to me as though the way the entity type and bundle have been set up needs a bit of work to work with Search API.

I'd like to write a patch but, after a lot of head scratching, I don't understand the way things have been set up.

Cheers,

Andrew

Andrew Edwards’s picture

Category: feature » bug
alan d.’s picture

Category: feature » bug

Does #1458938: Add schema columns properties help?

As an aside, this seems to be wrong imho, but I have never really investigated but I think it is the root cause for a lot of issues.

  // Do not use the type "country" here because that would reference the entity
  // type. THIS BREAKS THE ENTITY TO ENTITY RELATIONSHIP
  $property['type'] = ($field['cardinality'] != 1) ? 'list<countries>' : 'countries';

From some reading done months back, I thought that this should be the entity type name

i.e.

  $property['type'] = ($field['cardinality'] != 1) ? 'list<country>' : 'country';
Andrew Edwards’s picture

Interesting! Yes #1458938: Add schema columns properties does help. I've adjusted the patch on that issue to work with the latest dev and now Countries seems to be working with Search API. Excellent!

The code you mention:

<?php
  // Do not use the type "country" here because that would reference the entity
  // type. THIS BREAKS THE ENTITY TO ENTITY RELATIONSHIP
  $property['type'] = ($field['cardinality'] != 1) ? 'list<countries>' : 'countries';
?>

has actually been removed in the patch, which seems a better approach to me.

I'll post a new patch on the other issue after I've done a bit more testing.

alan d.’s picture

Status: Active » Fixed

Tentatively marking a duplicate of #1458938: Add schema columns properties.

Reopen if this does not solve this issue.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

stone_d’s picture

Issue summary: View changes

Sorry to re-open this but ...

... this is still not working for me. I Get the ISO2-code-names as facets. I can't find a way to change the display name to the full located country name anywhere.
Are there any hints on this issue?

Thanks and best regards

alan d.’s picture

@stone_d
Are you using the latest dev release? And does that have the patch from the other issues?

stone_d’s picture

StatusFileSize
new13.2 KB

Hi Alan,

thanks for the quick reply! I actually run the latest dev release 7.x-2.1+24-dev with NO additional patchings.
I attached how my dropdown looks like in the country-facet of search-api.

alan d.’s picture

Status: Closed (fixed) » Needs work

latest dev has this... reopening :(

stone_d’s picture

Wouldn't it be perfect to have an additional setting in the Facet-Display-Settings where u can choose how you want the country to be outputted and maybe also offer the custom fields of the country-entity to be selected for display? Then you could also use flags, or any image you like.

... just as an idea - for me the language-dependent country name would be totally enough!

stone_d’s picture

Without stressing: Is there any progress on this?
I just wanna know since if not - i have to switch for another solution.

Thanks

alan d.’s picture

I don't use the search api, so personally can not test, debug, resolve....

If you want to try things, I can make suggestions.

a) Workaround: A form alter on that form and repopulate from the countries list (reloop through the options and set the values as the country label)

function aaa_form_bbbbbb_alter(&$form, $form_state) {
$enabled_countries = countries_get_countries('name', array('enabled' => COUNTRIES_ENABLED));
// This would nuke empty options potentially, depending on if the #empty_option is used or if part of the #options itself.
$form['xxx']['zzzz']['#options'] = array_intersect_keys($enabled_countries, $form['xxx']['zzzz']['#options']);
}

b) Fix internally
...??

johnv’s picture

Just for inspiration: this is how the Workflow module solves this issue using hook_entity_property_info_alter().

But probably you need to use hook_field_info() or hook_field_info_alter().
https://api.drupal.org/api/drupal/modules%21field%21field.api.php/functi...
https://drupal.org/node/1156554

alan d.’s picture

Yuks, we don't need to define something like this?

http://drupalcode.org/project/entity.git/blob/refs/heads/7.x-1.x:/module...

:?

muschpusch’s picture

Have at the patch and comments here: #1803930: Making Node Weight expose it's data to entity API I'm working on something similar for the weight module.

mrded’s picture

You can also rewrite facet items with facetapi_bonus module.

function HOOK_facet_items_alter(&$build, &$settings) {
  if ($settings->facet == "YOUR_FACET_NAME") {
    foreach ($build as $key => $item) {
      $build[$key]["#markup"] = country_load($item["#markup"])->name;
    }
  }
}
stone_d’s picture

oh - well ... i gotta try this! Thanks!

ptmkenny’s picture

I'm not sure if I should open a new issue, but has anyone had success getting the country displayed in Views when indexed by search API? When I add the country as a field, I can choose from

* Show entity label
* Show entity ID
* Show complete entity (View modes: Country, Revision comparison, Tokens)

But none of these actually show me anything in my View. Any ideas would be much appreciated.

ptmkenny’s picture

Ok, to get this in a View, I made a template. I wanted the country icon so I added this in the relevant template file:

<?php

$output = $row->_entity_properties['relation_my_content_type_node:field_nationality'];
print theme('countryicons_icon', array('code' => $output, 'iconset' => 'shiny'));

For whatever reason, the views UI does not work as is, but the two-letter country code can be pulled in by template.

alan d.’s picture

Yes, it is much better to open a new thread, but since you are here already...

Do you have the option to create a relationship from the Entity to the country (Addvanced > Relationships)? If so, then you get all of the field settings for free :)

If not, create a new issue and I will try and tack down a snippet of sample code that may work.