5

In D6 and D7, with a simple form_alter you could unset $element['format'], but now the format help and the format tips are added in a #process method.

How can I remove them?

1
  • Form alter and replace the #process callback with a custom one maybe?
    – Clive
    Jan 23, 2015 at 16:07

1 Answer 1

4

Apparently you can't hook into the #process phase, but you can still do a #pre_render, from /Drupal/Core/Render/Renderer.php

// If the default values for this element have not been loaded yet, populate
// them.
if (isset($elements['#type']) && empty($elements['#defaults_loaded'])) {
  $elements += $this->elementInfo->getInfo($elements['#type']);
}

// Make any final changes to the element before it is rendered. This means
// that the $element or the children can be altered or corrected before the
// element is rendered into the final text.
if (isset($elements['#pre_render'])) {
  foreach ($elements['#pre_render'] as $callable) {
    if (is_string($callable) && strpos($callable, '::') === FALSE) {
      $callable = $this->controllerResolver->getControllerFromDefinition($callable);
    }
    $elements = call_user_func($callable, $elements);
  }
}

For example, to hide the element format of the body field of an article:

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form['#pre_render'][] = 'mymodule_form_pre_render_body';
}

function mymodule_form_pre_render_body(array $element) {
  foreach (Element::children($element) as $key) {
    if ($key == 'body') {
      foreach (Element::children($element[$key]['widget']) as $widget) {
        $element[$key]['widget'][$widget]['format']['#access'] = FALSE;
      }
    }
  }

  return $element;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.