You are here

function ajax_comments_entity_display_build_alter in AJAX Comments 8

Implements hook_entity_display_build_alter().

File

./ajax_comments.module, line 154
AJAX comments module file.

Code

function ajax_comments_entity_display_build_alter(&$build, $context) {
  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 (isset($build[$field_name])) {
          $build[$field_name]['#attributes']['id'] = Html::getUniqueId($html_id);
          Utility::setEntityRenderArray($build, $context['entity'], $context['display']
            ->getMode());
        }
      }
    }
  }
}