For my project I need to use the RoyalSlider Integration module to have pictures appear in a full screen slideshow. All is working ok, the only thing is that I want to alter its input form when you want to add new content (node/add). The module is great - you can add images but you can also add Youtube videos to the slider. Because I only want to upload images with it, I want to remove the video form element because it's simply not needed. I want to have my input forms clean so I created a custom module that removes the video element from the form.

I used the hook_field_widget_WIDGET_TYPE_form_alter function for it. At first sight, it seems to be ok. The form elements got removed but when you edit the node, all the input is gone (as in: I can't see what files I used to upload).
My question, what am I doing wrong and how can I load its values in the form fields?

My custom module code:

function custom_royalslider_field_widget_royalsliderfields_widget_form_alter(&$element, &$form_state, $context, $delta) {
  $element['rs_image'] = array(
    '#title' => t('Image'),
    '#description' => t('Allowed extensions: gif png jpg jpeg'),
    '#type' => 'managed_file',
    '#required' => TRUE,
    '#default_value' => isset($items[$delta]['rs_image']) ? $items[$delta]['rs_image'] : '',
    '#upload_location' => 'public://royalslider_images',
    '#upload_validators' => array(
      'file_validate_extensions' => array('gif png jpg jpeg'),
    ),
  );

  unset($element['rs_caption']);
  unset($element['rs_video']);
  unset($element['rs_link']);  

  return $element;
}

The form snippet from the RoyalSlider Integration module I want to alter:

function royalsliderfield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $required = ($instance['required'] && $delta == 0) ? 1 : 0;
  $required_field = isset($instance['settings']['required_field']) ? $instance['settings']['required_field'] : 'rs_image';

  $element['rs_image'] = array(
    '#title' => t('Image'),
    '#description' => t('Allowed extensions: gif png jpg jpeg'),
    '#type' => 'managed_file',
    '#required' => ($required && $required_field == 'rs_image') ? TRUE : FALSE,
    '#default_value' => isset($items[$delta]['rs_image']) ? $items[$delta]['rs_image'] : '',
    '#upload_location' => 'public://royalslider_images',
    '#upload_validators' => array(
      'file_validate_extensions' => array('gif png jpg jpeg'),
    ),
  );
  return $element;

Comments

atiba’s picture

Ok, solved my problem.
The default values were pointing to variables that weren't there.
With some var dumping on $context I found the right values. So I added:

$delta = $context['delta'];
$items = $context['items'];

And removed $delta from the function call and everything works now!