I've build a site that uses Search API and I want to randomly have the returned content from the main search page's content hierarchy every 24hrs as want to have it sorted thru Views using their weights. I will use Random Weights for the randomization.

I can get this done normally thru views I just want to get it into my Search Index which right now Search API doesn't recognize Weight as a recordable index (see attached) I see all the other aspects of the node except the weight that I can apply as an index.

Is there a way that I can add Weight to my Search API index?

Thanks for the help.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

davisben’s picture

Priority: Major » Normal
Status: Active » Closed (won't fix)

Unfortunately I don't think this is possible. In looking at Search API, it appears to use entity properties and fields, or which the weight is neither.

muschpusch’s picture

Title: Making Node Weight able to be seen by Search API Index » Making Node Weight expose it's data to entity API
Version: 7.x-2.1 » 7.x-2.x-dev
Issue summary: View changes
Status: Closed (won't fix) » Needs review
FileSize
963 bytes

Actually this is a valid feature request :) I changed the title since supporting entity API should fix: rules, search api, restws (etc) integration. Please review and test the attached patch i only tested with restws

muschpusch’s picture

Ok there was a problem when creating a node using entity API. See the updated patch for a fix

muschpusch’s picture

I'm actually not sure if it's safe what i'm doing in the last patch. Created an issue in the entity API queue: #2202379: make NID available in setter callback

joachim’s picture

D.org ate my last comment.

Briefly:

- the hook can go in a .inc file.
- you should not be saving the entity when the property is being set. That is not how wrappers are meant to work.

joachim’s picture

To expand on that:

You should just be setting your data on the $data, which is the node.

If the user of the wrapper wants to save the node afterwards, they will do so.

At that point, hook_node_insert/update() is invoked, and your implementations of that will deal with saving your data to your table.

muschpusch’s picture

Thanks a lot joachim! New patch attached.

joachim’s picture

Status: Needs review » Needs work
+++ b/weight.module
@@ -1,6 +1,40 @@
+function weight_set_properties(&$data, $name, $value, $langcode, $type, $info){
+  $data->weight_weight = $value;
+
+  // skip the execution now and let hook_node_insert handle it
+  if ($data->is_new == TRUE && !isset($data->nid)){
+    return true;
+  }
+  _weight_set_weight($data);
+}

This isn't quite right yet.

You need to set the data on the $node in all circumstances.

You should not be saving the node yourself -- or your own data -- in any circumstance.

muschpusch’s picture

Hey Joachim, _weight_set_weight($data); actually only saves in the custom weight table.

You need to set the data on the $node in all circumstances.

I do that

You should not be saving the node yourself

I don't do that anymore

[...] -- or your own data -- in any circumstance.

How else should i tell entity API to save the value ?!?

joachim’s picture

> Hey Joachim, _weight_set_weight($data); actually only saves in the custom weight table.

You should not be saving anything at all to the DB in response to a property being set. That's not the job of the setter.

You should change the $node in some way, for your implementation of hook_node_insert/update() to notice and deal with when the user chooses to save the node.

> How else should i tell entity API to save the value ?!?

Not the job of the setter callback. It's the responsibility of the user of the entity wrapper to save the node themselves.

muschpusch’s picture

Assigned: Unassigned » muschpusch

Quit busy right now but will work on this end of the week...

MegaChriz’s picture

Assigned: muschpusch » Unassigned
Status: Needs work » Needs review
FileSize
1.08 KB

The attached patch addresses the issues noted in #8. I also removed whitespace, renamed the setter and getter callbacks and renamed the first parameter from the setter callback ($data) to $node to be consistent with the getter callback.

MegaChriz’s picture

+++ b/weight.module
@@ -89,6 +89,33 @@ function weight_page_default() {
+ * Implements hook_entity_property_info().

Just noticed that function docs for weight_entity_property_info_alter() are not right. This should be Implements hook_entity_property_info_alter() instead of Implements hook_entity_property_info().

New patch.

deggertsen’s picture

Patch #13 worked great for me. I'm now able to use the weight field in my Search API index. Thanks!

mglaman’s picture

Status: Needs review » Reviewed & tested by the community

Marking as RTBC based on #14!

argiepiano’s picture

Thank you for the patch. In addition, with this patch, the Weight field of a node is now exposed to Rules and therefore can be set and changed.

davisben’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks for the patch!

  • davisben committed 6f05652 on 7.x-2.x authored by MegaChriz
    Issue #1803930 by MegaChriz, muschpusch: Expose data to Entity API.
    

Status: Fixed » Closed (fixed)

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