1

I have a content type with an image field. Users can create the content and upload an image. I'd like to not allow users to change/remove the image once it was uploaded, but still show the image on the node edit form. So I only need to disable/remove the 'remove' button from the image field. I tried the following (via hook_form_alter), but it didn't work:

$form['field_image']['#disabled'] = TRUE;

The below works, but it hides the image element completely, which is not what I'm after:

$form['field_image']['#access'] = FALSE;

Please help to find a workaround.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Alexis
  • 83
  • 2
  • 11

2 Answers2

5

You have to use the hook_field_widget_form_alter function and inside there look for the variable details using dpm() and then alter the button using an attribute from the Forms API.

But I would suggest to make the widget field read only on the edit form and not remove the delete button.

// Hide remove button from an image field
function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  // If this is an image field type
  if ($context['field']['field_name'] == 'MY_FIELD_NAME') {
    // Loop through the element children (there will always be at least one).
    foreach (element_children($element) as $key => $child) {
      // Add the new process function to the element
      $element[$key]['#process'][] = 'MYMODULE_image_field_widget_process';
    }
  }
}

function MYMODULE_image_field_widget_process($element, &$form_state, $form) {
  //dpm($element);
  // Hide the remove button
  $element['remove_button']['#type'] = 'hidden';

  // Return the altered element
  return $element;
}

Useful issues:

Community
  • 1
  • 1
TheodorosPloumis
  • 2,396
  • 1
  • 17
  • 31
  • Thanks a lot Theodoros! the hook above works very well. I didn't get though how to disable it as per you suggestion. I tried $form['field_image']['und']['#attributes']['disabled'] = TRUE, but it didn't work. – Alexis Sep 11 '13 at 15:28
  • In this case do a **dpm($form)** inside your hook_form_alter (because now you are using the whole form) and find the proper tree of the variable. I think you are missing the 'value': (`$form['field_image']['und']['0']['value']['#attributes']['disabled'] = TRUE;`) – TheodorosPloumis Sep 12 '13 at 10:12
1

You can also do it using hook_form_alter and after_build function

// hook_form_alter implementation
function yourmodule_form_alter(&$form, $form_state, $form_id) {
    switch ($form_id)  {
        case 'your_form_id':
            $form['your_file_field'][LANGUAGE_NONE]['#after_build'][] = 'yourmodule_hide_remove_button';
            break;
    }
}

// after_build function
function yourmodule_hide_remove_button($element, &$form_state) {
    // if multiple files are allowed in the field, then there may be more than one remove button.
    // and we have to hide all remove buttons, not just the one of the first file of the field
    // 
    // Array
    // (
    //    [0] => 0
    //    [1] => 1
    //    [2] => #after_build
    //    [3] => #field_name
    //    [4] => #language
    //    [5] => ...
    // )
    //
    // the exemple above means we have 2 remove buttons to hide (2 files have been uploaded)

    foreach ($element as $key => $value){
        if (is_numeric($key)){
            unset($element[$key]['remove_button']);
        } else break;
    }

    return $element;
}
Henry
  • 593
  • 2
  • 5
  • 12
  • Is this not working with https://www.drupal.org/project/multifield. Is there anything else about multifield compare to file field or field collection? – kiranking Mar 25 '15 at 17:47