You are here

function social_ajax_comments_entity_display_build_alter in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_entity_display_build_alter()
  2. 8.8 modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_entity_display_build_alter()
  3. 10.3.x modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_entity_display_build_alter()
  4. 10.1.x modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_entity_display_build_alter()
  5. 10.2.x modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_entity_display_build_alter()

Implements hook_entity_display_build_alter().

File

modules/custom/social_ajax_comments/social_ajax_comments.module, line 24
The Social AJAX comments module.

Code

function social_ajax_comments_entity_display_build_alter(&$build, $context) {

  // Only for comments on nodes on the default display.
  // This is to override the defaults in ajax_comments who
  // don't take into account correct wrapper and html id's
  // for our post and nodes.
  if (!$context['entity'] instanceof Node && !$context['entity'] instanceof Post) {
    return;
  }
  foreach ($build as $field_name => $field) {
    if (!empty($field['#field_type']) && $field['#field_type'] === 'comment') {

      // Check that this comment field uses Ajax Comments.

      /** @var \Drupal\ajax_comments\FieldSettingsHelper $field_settings_helper */
      $field_settings_helper = \Drupal::service('ajax_comments.field_settings_helper');
      $field_config = $build[$field_name]['#items']
        ->getFieldDefinition();
      $field_formatter = $field_settings_helper
        ->getFieldFormatter($context['display'], $field['#field_name'], $field_config, $context['display']
        ->getMode());
      if (!empty($field_formatter) && $field_settings_helper
        ->isEnabled($field_formatter)) {

        // Check if this ID is being generated in response to an Ajax request.
        if (Utility::isAjaxRequest(\Drupal::request())) {

          // Note that setting ajax as TRUE on Html here also fixes issue
          // with non-unique IDs on textarea elements return through ajax,
          // which otherwise could result in WYYSIWYG editors being
          // incorrectly attached by Drupal.attachBehaviors().
          Html::setIsAjax(TRUE);
        }
        $html_id = $field['#entity_type'] . '_' . $field['#bundle'] . '_' . $field['#field_name'];
        if ($context['entity'] instanceof Post) {

          // It will also run on AJAX commands so cant do a .=
          // otherwise it will keep on adding the id.
          $html_id = $field['#entity_type'] . '_' . $field['#bundle'] . '_' . $field['#field_name'] . '_' . $context['entity']
            ->id();
        }
        if (isset($build[$field_name])) {
          $build['#cache']['contexts'][] = 'url';
          $build[$field_name]['#attributes']['id'] = Html::getId($html_id);
          Utility::setEntityRenderArray($build, $context['entity'], $context['display']
            ->getMode());
        }
      }
    }
  }
}