0

I am working on a site that uses the Chosen module. There is a field that I need to edit programatically. When I use the actual field edit interface, Chosen adds an option called "Apply Chosen to the select fields in this widget?" When I set that value and submit the form, all is well... it changes what I need changed. However, I need to be able to track down what this is actually doing on the back end so I can change the value of this option for this field programatically... once... in an update hook. The user needs to still be able to edit this value using the form... I just want to flip its value this one time. I don't know where to start looking. In the Chosen module, I can see where it adds the option to the form, but I don't know what piece of data that option is stored with, nor how to see what the form is doing when it saves it so I can duplicate that functionality. It's hard to even know the right terminology to ask this question.

3
  • Chosen module doesn't change anything for the field in the backend. It just applies the Chosen Jquery plug-in to the select form element.
    – Elin Y.
    Commented Feb 28, 2016 at 8:19
  • What you need to do here is update the settings for the field instance. This is where the chosen settings are stored. If you use field_info_instance to retrieve be field data for a field you have configured to use chosen you will be able to see. If you rewrite the question to be more specific, i.e. how to programmatically enable Chosen for a field, I will provide an example.
    – ratface
    Commented Feb 28, 2016 at 8:49
  • Ah, ok, now I understand what you actually mean.
    – Elin Y.
    Commented Feb 28, 2016 at 8:57

1 Answer 1

1

You mean updating the field instance settings programmatically, in an update hook, hook_update_N(). Do it as follows:

function YOURMODULE_update_N(&$sandbox) {
  // First load the field instance.
  $instance = field_info_instance($entity_type, $field_name, $bundle);
  // Make the changes in the field instance configuration.
  $instance['widget']['settings']['apply_chosen'] = 1;
  // Save the field instance.
  field_update_instance($instance);
}
3
  • This is exactly right, as I found out! One question: what is the different between field_info_instance() and field_read_instance()? They seem to do the exact same thing in this case. Commented Feb 29, 2016 at 19:41
  • I'd recommend you to look at the Drupal API Documentation first.
    – Elin Y.
    Commented Mar 1, 2016 at 6:00
  • "Generally, you should use field_info_instance() instead, as it provides caching and allows other modules the opportunity to append additional formatters, widgets, and other information."
    – Elin Y.
    Commented Mar 1, 2016 at 6:02

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.