2

I have a multi-value field added to my content type. It is actually a field collection.

The users can add unlimited items to this field, and rearrange the items using Drupal's drag-and-drop utility.

However, I want to add some custom logic to the arrangement of field values according to the content.

Which hook should I use, to modify the arrangement of field values before the node saved into the database.

For example, I have the following structure:

My Content Type
|
|-- Field Collection Field // unlimited values
    |
    |-- Name // Text field
    |-- Surname // Text field
    |-- Company // Select list

Let's say a user created a content with the following values:

  • John - Doe - ACME
  • Jane - Doe - Ace
  • Bob - X - ACME
  • Fred - Something - Ace

I want to sort them with a custom logic in a hook, so that they are arranged like that:

  • Bob - X - ACME
  • John - Doe - ACME
  • Fred - Something - Ace
  • Jane - Doe - Ace

My question is NOT how to write the logic, BUT which hook to use to modify the arrangements so that I can put my function into it.

2
  • Just as a suggestion, would it make any sense to leave the reordering to display time (eg. via a custom formatter or a hook_field_attach_view_alter()? This could prove useful if you ever need to support multiple sort methods.
    – Andy
    Commented Jul 2, 2014 at 14:26
  • Thanks for the suggestion, but I don't think that I would ever need any other sort method. The reason why I want to do it before saving, is that the function called only once and all is good. If I implement it on display time, that means the function will be called every time the node is viewed, and it would impact the performance.
    – Elin Y.
    Commented Jul 2, 2014 at 14:29

2 Answers 2

1

I would suggest you hook_node_presave to modify node edit form submitted values before saving it into DB.

2
  • I think you could also use hook_field_attach_presave() if you wanted to be entity type agnostic.
    – Andy
    Commented Jul 2, 2014 at 14:18
  • @Andy this hook seems very interesting. I'll definitely try that as well.
    – Elin Y.
    Commented Jul 2, 2014 at 14:34
0

Attach a custom validate handler for the element and make sure it will be called first. Then in your validate function you can change the field weight in $form_state.

4
  • 1
    This would only work if the node is created using the form. It won't work if the nodes are created programmatically, for example using feeds.
    – Elin Y.
    Commented Jul 2, 2014 at 13:18
  • I have done something similar to this sometime back. If you are going to use hook_node_presage then changing the weight won't work, you might have to rearrange the array itself.
    – msmani
    Commented Jul 2, 2014 at 13:35
  • Thanks, I am planning to use usort and rearrange the array according to my custom logic.
    – Elin Y.
    Commented Jul 2, 2014 at 13:41
  • I am not sure whether it will work, let me know of you make it work
    – msmani
    Commented Jul 2, 2014 at 13:46

Your Answer

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

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