0

I want reverse of these https://www.drupal.org/project/drupal/issues/2098071 https://www.drupal.org/project/drupal/issues/1340860

It can be achieved by hacking core i,e removing line

$element['format']['guidelines'][$format_id]['#access'] = FALSE;

in Element.php but I want proper drupal solution.

5
  • 2
    Instead of hacking core you could add your own #pre_render callback to the text_format element. When you do this from a theme you can be sure that yours runs later.
    – 4k4
    Jun 17 '20 at 19:39
  • I can't change anything from a theme, because I need to display these guidelines below form element on node forms, which are in admin theme (seven theme)
    – neha
    Jun 18 '20 at 16:12
  • 1
    You could subtheme the admin theme as well, but if you don't need it for other things this would be totally unnecessary looking at your great answer :-).
    – 4k4
    Jun 18 '20 at 16:32
  • Thanks 4k4 for appreciating my answer. Sadly my team member does not like this piece of code and has requested hell lot of changes including to completely remove hook weight and instead to set module weight and so many changes. This is so demotivating.
    – neha
    Jun 22 '20 at 6:47
  • Yes, Drupal is quite confusing in handling hook priorities. Sometimes you use a different hook, a theme instead of a module, manipulate modules implemations or use module weights. We have to wait until hooks are replaced by events with a plain and simple priority value. Also your code contains some code duplications, but in this case this is not relevant for performance.
    – 4k4
    Jun 22 '20 at 6:57
2
namespace Drupal\my_module;

use Drupal\editor\Entity\Editor;
use Drupal\editor\Element;

/**
 * Defines a service for Text Editor's render elements.
 */
class CustomElement extends Element {

  /**
   * Additional #pre_render callback for 'text_format' elements.
   */
  public function preRenderTextFormat(array $element) {

    $element = parent::preRenderTextFormat($element);

    $format_ids = array_keys($element['format']['format']['#options']);

    // Early-return if no text editor is associated with any of the text formats.
    $editors = Editor::loadMultiple($format_ids);
    
    // Hide the text format's filters' guidelines of those text formats that have
    // a text editor associated: they're rather useless when using a text editor.
    foreach ($editors as $format_id => $editor) {
      // Display filter guidelines.
      $element['format']['guidelines'][$format_id]['#access'] = TRUE;
    }

    return $element;

function my_module_core_element_info_alter(&$types) {

  $types['text_format']['#pre_render'][] = 'my_module.element.editor:preRenderTextFormat';
}

function my_module_module_implements_alter(&$implementations, $hook) {
  switch ($hook) {
    case 'element_info_alter':
      $group = $implementations['my_module'];
      unset($implementations['my_module']);
      $implementations['my_module'] = $group;
      break;
  }
}

Above piece of code worked me and defined this service.

Your Answer

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

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