TransWikia.com

Hide Alt and Title for image field

Drupal Answers Asked by moto on February 20, 2021

How to hide Alt and Title fields for anonymus users when they upload images? I use tokens for these field that’s why I need to hide them.

2 Answers

Here a solution for hide the alt and title fields:

/**
 * Implements hook_field_widget_form_alter().
 */
function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
    // If this is an image field type
    if ('image' === $context['field']['type'] && 'MY_FIELD' === $context['instance']['bundle']) {
    // 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) {
    $element['title']['#access'] = FALSE;
    $element['alt']['#access'] = FALSE;

    // Return the altered element
    return $element;
}

Et voila :)

Answered by kxo on February 20, 2021

This could work with Field Permissions module...

Answered by Volker on February 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP