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!

Related Questions

KernelEvents::REQUEST is not fired on cached pages

2  Asked on December 8, 2021 by user34185


Add column to custom field schema

3  Asked on December 8, 2021 by coder1


Order receipt email content

1  Asked on December 8, 2021 by pete-l


Date time save in database

0  Asked on December 8, 2021 by pratip-ghosh


How can I create a recommended project in the root directory?

2  Asked on December 6, 2021 by user2574948


Wrapping fields together in a Views Exposed Filter

1  Asked on December 4, 2021 by typologist


Submit form and disable submission button

7  Asked on December 4, 2021 by matt9292


How to check if an entity is referenced before deleting?

2  Asked on December 2, 2021 by halofx


Add class to {{ link }} twig attribute

3  Asked on December 2, 2021


Remove field attributes using hook_form_alter()

1  Asked on November 30, 2021 by senthilkumar


How to send email to submitter in a webform?

2  Asked on November 30, 2021 by cel57


Ask a Question

Get help from others!

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