You are here

function social_ajax_comments_ajax_render_alter in Open Social 10.2.x

Same name and namespace in other branches
  1. 10.3.x modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_ajax_render_alter()
  2. 10.0.x modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_ajax_render_alter()
  3. 10.1.x modules/custom/social_ajax_comments/social_ajax_comments.module \social_ajax_comments_ajax_render_alter()

Implements hook_ajax_render_alter().

File

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

Code

function social_ajax_comments_ajax_render_alter(array &$data) {
  if ((count($data) === 2 || count($data) === 3) && $data[0]['command'] === 'settings' && $data[1]['command'] === 'insert' && $data[1]['selector'] === NULL && \Drupal::request()->query
    ->has('page')) {
    if ($data[1]['method'] === NULL) {
      $theme = \Drupal::service('theme.manager')
        ->getActiveTheme()
        ->getName();
      $data[1]['method'] = 'html';
      $data[1]['selector'] = '#block-' . $theme . '-content';
    }
    elseif ($data[1]['method'] === 'replaceWith' && preg_match('/<section[^>]+id="([^"]+)/', $data[1]['data'], $matches)) {
      $data[1]['selector'] = '#' . $matches[1];
    }
  }

  // Delete content before wrapper of the comments section of AJAX response
  // during adding a new comment to resolve the issue with creating nesting of
  // comments section wrapper in itself after adding each comment.
  if (\Drupal::routeMatch()
    ->getRouteName() === 'ajax_comments.add' && \Drupal::request()->request
    ->has('social_ajax_comments')) {
    foreach ($data as &$item) {
      if ($item['command'] === 'insert' && $item['method'] === 'html' && preg_match('/^#([a-z\\-]+)$/', $item['selector'], $matches)) {
        $content =& $item['data'];
        $content = substr($content, strrpos(substr($content, 0, strpos($content, $matches[1])), '<'));
        return;
      }
    }
  }
}