Skip to main content
improved formatting; Post Made Community Wiki
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

How tocan I set the default format in text_formatfor a node body field and hide the selectform element to choose it?

/**
 * Implements hook_form_alter().
 */
function MODULE_NAME_form_alter(
  &$form,
  \Drupal\Core\Form\FormStateInterface $form_state,
  $form_id
) {

  if ($form_id == 'node_article_form') {
    $form["body"]["widget"][0]['#allowed_formats'] = ['full_html']; // Your default format
    $form["body"]["widget"][0]['#after_build'][] = 'MODULE_NAME_hide_format';
  }
}

function MODULE_NAME_hide_format(
  &$element,
  FormStateInterface $form_state
) {
  if (isset($element["format"]["format"])) {
    $element["format"]["format"]["#access"] = FALSE;
  }

  return $element;
}

I am trying to set the default input format for a node body field and avoid users can select a different input format by hiding the form element to change it.

I am using the following code, but it doesn't work.

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'node_article_form') {
    $form["body"]["widget"][0]['#allowed_formats'] = ['full_html'];
    $form["body"]["widget"][0]['#after_build'][] = 'mymodule_hide_format';
  }
}

function mymodule_hide_format(&$element, FormStateInterface $form_state) {
  if (isset($element["format"]["format"])) {
    $element["format"]["format"]["#access"] = FALSE;
  }

  return $element;
}

How to set the default format in text_format and hide the select?

/**
 * Implements hook_form_alter().
 */
function MODULE_NAME_form_alter(
  &$form,
  \Drupal\Core\Form\FormStateInterface $form_state,
  $form_id
) {

  if ($form_id == 'node_article_form') {
    $form["body"]["widget"][0]['#allowed_formats'] = ['full_html']; // Your default format
    $form["body"]["widget"][0]['#after_build'][] = 'MODULE_NAME_hide_format';
  }
}

function MODULE_NAME_hide_format(
  &$element,
  FormStateInterface $form_state
) {
  if (isset($element["format"]["format"])) {
    $element["format"]["format"]["#access"] = FALSE;
  }

  return $element;
}

How can I set the default format for a node body field and hide the form element to choose it?

I am trying to set the default input format for a node body field and avoid users can select a different input format by hiding the form element to change it.

I am using the following code, but it doesn't work.

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'node_article_form') {
    $form["body"]["widget"][0]['#allowed_formats'] = ['full_html'];
    $form["body"]["widget"][0]['#after_build'][] = 'mymodule_hide_format';
  }
}

function mymodule_hide_format(&$element, FormStateInterface $form_state) {
  if (isset($element["format"]["format"])) {
    $element["format"]["format"]["#access"] = FALSE;
  }

  return $element;
}
Source Link
user83311
user83311

How to set the default format in text_format and hide the select?

/**
 * Implements hook_form_alter().
 */
function MODULE_NAME_form_alter(
  &$form,
  \Drupal\Core\Form\FormStateInterface $form_state,
  $form_id
) {

  if ($form_id == 'node_article_form') {
    $form["body"]["widget"][0]['#allowed_formats'] = ['full_html']; // Your default format
    $form["body"]["widget"][0]['#after_build'][] = 'MODULE_NAME_hide_format';
  }
}

function MODULE_NAME_hide_format(
  &$element,
  FormStateInterface $form_state
) {
  if (isset($element["format"]["format"])) {
    $element["format"]["format"]["#access"] = FALSE;
  }

  return $element;
}