Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#13 | weight-entity-property-api-1803930-13.patch | 1.09 KB | MegaChriz |
weight.jpg | 59.13 KB | lamair |
Comments
Comment #1
davisbenUnfortunately 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.
Comment #2
muschpusch CreditAttribution: muschpusch commentedActually 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
Comment #3
muschpusch CreditAttribution: muschpusch commentedOk there was a problem when creating a node using entity API. See the updated patch for a fix
Comment #4
muschpusch CreditAttribution: muschpusch commentedI'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
Comment #5
joachim CreditAttribution: joachim commentedD.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.
Comment #6
joachim CreditAttribution: joachim commentedTo 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.
Comment #7
muschpusch CreditAttribution: muschpusch commentedThanks a lot joachim! New patch attached.
Comment #8
joachim CreditAttribution: joachim commentedThis 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.
Comment #9
muschpusch CreditAttribution: muschpusch commentedHey Joachim, _weight_set_weight($data); actually only saves in the custom weight table.
I do that
I don't do that anymore
How else should i tell entity API to save the value ?!?
Comment #10
joachim CreditAttribution: joachim commented> 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.
Comment #11
muschpusch CreditAttribution: muschpusch commentedQuit busy right now but will work on this end of the week...
Comment #12
MegaChriz CreditAttribution: MegaChriz commentedThe 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.Comment #13
MegaChriz CreditAttribution: MegaChriz commentedJust noticed that function docs for
weight_entity_property_info_alter()
are not right. This should beImplements hook_entity_property_info_alter()
instead ofImplements hook_entity_property_info()
.New patch.
Comment #14
deggertsen CreditAttribution: deggertsen commentedPatch #13 worked great for me. I'm now able to use the weight field in my Search API index. Thanks!
Comment #15
mglamanMarking as RTBC based on #14!
Comment #16
argiepiano CreditAttribution: argiepiano commentedThank 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.
Comment #17
davisbenCommitted. Thanks for the patch!