You are here

function ajax_comments_entity_view_alter in AJAX Comments 8

Implements hook_entity_view_alter().

File

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

Code

function ajax_comments_entity_view_alter(array &$build, ContentEntityInterface $entity, EntityViewDisplayInterface $display) {
  $commands_added =& drupal_static(__FUNCTION__);
  if (!isset($commands_added)) {
    $commands_added = FALSE;
  }
  if (!$commands_added) {
    foreach ($entity
      ->getFieldDefinitions() as $machine_name => $field_config) {
      if ($field_config
        ->getType() === 'comment') {

        /** @var \Drupal\ajax_comments\FieldSettingsHelper $field_settings_helper */
        $field_settings_helper = \Drupal::service('ajax_comments.field_settings_helper');
        $field_formatter = $field_settings_helper
          ->getFieldFormatter($display, $machine_name, $field_config, $display
          ->getMode());
        if (!empty($field_formatter) && $field_settings_helper
          ->isEnabled($field_formatter)) {
          $build['#attached']['library'][] = 'ajax_comments/commands';
          $commands_added = TRUE;
          break;
        }
      }
    }
  }
}