I am trying to do something like :

 * Implements hook_update_N().
function module_name_update_7002() {
 // Change field to required

To alter a field to make it required in production (where i can't access the backoffice).

Which variable do I have to set for doing so?

closed as too broad by kiamlaluno Nov 9 '15 at 15:41

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


You may use following code to update your field

// Fetch an instance info array.
$instance_info = field_info_instance($entity_type, $field_name, $bundle_name);
// Change a single property in the instance definition.
$instance_info['required'] = TRUE;
// Write the changed definition back.

Get the form_id of that form and write an alter function for it in your custom module.

function hook_form_form_id_alter() {
 $form['that_field_name'] = array(
   '#required' => TRUE,
    // add other features you want to add

Follow the below link for more details: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_FORM_ID_alter/7

  • But for the form ID to be called, you have to visit the page. I need it to be change when running a update. – Ismail H Nov 9 '15 at 13:56
  • You want to update a form field to a required field, right? Then, in that case without using form_id, how can you make the changes? Is this the same case you posted about? – Kiran Tej Nov 9 '15 at 14:05
  • I think that the way you described it works fine for me. But I don't want to write a code that will be launched each time If there is a way to write a function inside a hook_update_N(). Your answer works fine and if There are no other choice I'll end up doing it. It's just that the site that I am working on is already slowing and I am trying not to charge it more. – Ismail H Nov 9 '15 at 14:15

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