0

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?

| improve this question | | | | |
1

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.
field_update_instance($instance_info);
| improve this answer | | | | |
1

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

| improve this answer | | | | |
  • 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.