You are here

function social_core_filter_process_format in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/social_core.module \social_core_filter_process_format()
  2. 8 modules/social_features/social_core/social_core.module \social_core_filter_process_format()
  3. 8.2 modules/social_features/social_core/social_core.module \social_core_filter_process_format()
  4. 8.3 modules/social_features/social_core/social_core.module \social_core_filter_process_format()
  5. 8.4 modules/social_features/social_core/social_core.module \social_core_filter_process_format()
  6. 8.5 modules/social_features/social_core/social_core.module \social_core_filter_process_format()
  7. 8.6 modules/social_features/social_core/social_core.module \social_core_filter_process_format()
  8. 8.7 modules/social_features/social_core/social_core.module \social_core_filter_process_format()
  9. 8.8 modules/social_features/social_core/social_core.module \social_core_filter_process_format()
  10. 10.0.x modules/social_features/social_core/social_core.module \social_core_filter_process_format()
  11. 10.1.x modules/social_features/social_core/social_core.module \social_core_filter_process_format()
  12. 10.2.x modules/social_features/social_core/social_core.module \social_core_filter_process_format()

Remove ability of selecting format if full_html is available.

@todo Instead of defining the list of fields it would be better to add a separate Setting to all text_format fields, allowing to select whether to show format selector or not.

1 string reference to 'social_core_filter_process_format'
social_core_element_info_alter in modules/social_features/social_core/social_core.module
Implements hook_element_info_alter().

File

modules/social_features/social_core/social_core.module, line 552
The Social core module.

Code

function social_core_filter_process_format($element) {

  // Only fields listed here will have text format settings disabled.
  $full_html_field_ids = [
    'edit-body-0',
    'edit-field-profile-self-introduction-0',
    'edit-field-group-description-0',
  ];
  $element_id = $element['#id'];

  // Check if there is generated sub-id and cleanup it.
  if (($sub_id = strpos($element_id, '--')) !== FALSE) {
    $element_id = substr($element_id, 0, $sub_id);
  }

  // Default filter format for text area fields.
  $social_filter_format = 'full_html';

  // Allow module to override default filter format because some content type
  // could require specific one.
  Drupal::moduleHandler()
    ->alter('social_filter_format_default', $social_filter_format);

  /** @var \Drupal\filter\Entity\FilterFormat $social_filter_format */
  $social_filter_format = FilterFormat::load($social_filter_format);

  // Check if filter format was loaded, to prevent fatal errors if wrong value
  // was provided in the hook_social_filter_format_default_alter().
  if (!$social_filter_format instanceof FilterFormatInterface) {
    return $element;
  }
  $permission_name = $social_filter_format
    ->getPermissionName();
  $account = Drupal::currentUser();
  if ($element['#type'] === 'text_format' && $element['#format'] === $social_filter_format
    ->id() && !$account
    ->hasPermission($permission_name)) {
    $element['#format'] = 'basic_html';
    $element['value']['#format'] = 'basic_html';
    $element['format']['format']['#default_value'] = 'basic_html';
    $element['format']['format']['#value'] = 'basic_html';
    $element['value']['#disabled'] = FALSE;
    $element['format']['format']['#access'] = FALSE;
    $element['format']['#access'] = TRUE;
    $key = array_search('filter_form_access_denied', $element['value']['#pre_render']);
    if (isset($element['value']['#pre_render'][$key])) {
      unset($element['value']['#pre_render'][$key]);
    }
  }
  elseif ($element['#type'] === 'text_format' && $account
    ->hasPermission($permission_name) && in_array($element_id, $full_html_field_ids)) {
    $element['#format'] = $social_filter_format;
    $element['format']['format']['#access'] = FALSE;
    $element['format']['format']['#value'] = $social_filter_format
      ->id();
    $element['format']['help']['#access'] = FALSE;
    $element['format']['format']['#options'] = [
      $social_filter_format
        ->id() => $social_filter_format
        ->label(),
    ];
  }
  return $element;
}