I can make form field disabled with hook_form_alter.

But can I somehow set form field disabled in Controller?

        namespace Drupal\mymodule\Controller;

        use Drupal\Core\Controller\ControllerBase;

        class addController extends ControllerBase {

           public function build() {

             $entity = $this->entityTypeManager()->getStorage('node')->create(array(
               'type' => 'someContentType',

             $form = $this->entityFormBuilder()->getForm($entity);
             //$form['field_link_to_item']['#disabled'] = 'disabled'; // <- not working

             return $form;

  • 2
    No, this is not possible, you have to do this in a form alter hook before the form is built. Specifically for entity forms there is a better solution. Provide as second parameter a form operation you have added to the entity type and configure a form mode by the same name in UI with the field removed. See drupal.stackexchange.com/questions/255419/… – 4k4 Mar 30 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.