-
Using hook_form_alter() and #after_build to set maxlength on emvideo title field
A fellow Drupal user posted an issue to the Embedded Media Field project. The user was trying to set the maxlength from 35 characters to some other number for the ‘title’ field within each embedded video widget. I faced the same issue with a site. The client wanted to be able to add more lengthy captions to each video they uploaded. I added the embedded video field (YouTube) as an “unlimited” video field.
The solution involves using hook_form_alter() and the #after_build property.
1234567891011121314151617181920212223242526272829/*** Implementation of hook_form_alter().*/function mymodule_form_alter(&$form, &$form_state, $form_id) {switch ($form_id) {case 'vgallery_node_form':$form['#after_build'][] = '_vgallery_after_build';break;}}/*** Custom after_build callback handler for the video gallery node form*/function _vgallery_after_build($form, &$form_state) {_vgallery_set_title_maxlength($form['field_vgallery_videos']);return $form;}/*** Set the maxlength on the title field of a video gallery emvideo element*/function _vgallery_set_title_maxlength(&$elements) {foreach (element_children($elements) as $key) {if (isset($elements[$key]['emvideo']['title'])) {$elements[$key]['emvideo']['title']['#maxlength'] = 255;}}}