View source
<?php
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
function comment_ajax_pager_entity_display_build_alter(&$build, $context) {
foreach ($build as $field_name => $field) {
if (!empty($field['#field_type']) && $field['#field_type'] === 'comment') {
$build[$field_name]['#attributes']['data-ajax_comment_pager'] = $context['entity']
->id();
$build[$field_name]['#attached']['library'][] = 'comment_ajax_pager/comment_ajax_pager.js';
$build[$field_name]['#attached']['drupalSettings']['comment_ajax_pager'][$context['entity']
->id()] = [
'ajax_pager' => [
'entity_id' => $context['entity']
->id(),
'field_name' => $field['#field_name'],
'entity_type' => $field['#entity_type'],
'bundle' => $field['#bundle'],
'comment_type' => $field['#items']
->getFieldDefinition()
->getSetting('comment_type'),
'default_mode' => $field['#items']
->getFieldDefinition()
->getSetting('default_mode'),
],
'ajax_url' => Url::fromRoute('comment_ajax_pager.api')
->toString(),
];
$config = \Drupal::config('field.field.' . $field['#entity_type'] . '.' . $field['#bundle'] . '.' . $field['#field_name'])
->get();
$build[$field_name][0]['comments']['#prefix'] = '<div class="comments_ajax_pager_wrap">';
$build[$field_name][0]['comments']['#suffix'] = '</div>';
if (!empty(\Drupal::config('comment_ajax_pager.settings')
->get('settings.' . $field['#entity_type'] . '.' . $field['#bundle'] . '.' . $field['#field_name'] . '.on')) && !empty($build[$field_name][0]['comments']['pager'])) {
$text = \Drupal::config('comment_ajax_pager.settings')
->get('settings.' . $field['#entity_type'] . '.' . $field['#bundle'] . '.' . $field['#field_name'] . '.text');
if (empty($text)) {
$text = 'Load more';
}
$text = t($text, [
'@per_page@' => $config['settings']['per_page'],
]);
$build[$field_name][0]['comments']['pager']['#prefix'] = '<div class="comment_load_more_pager" data-text="' . $text . '">';
$build[$field_name][0]['comments']['pager']['#suffix'] = '</div>';
}
}
}
}
function comment_ajax_pager_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
switch ($form_id) {
case 'field_config_edit_form':
if (!empty($form['settings']['per_page'])) {
$entity = $form['#entity'];
$entityTypeId = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
$field_name = $form['default_value']['widget']['#field_name'];
$form['settings']['load_more'] = [
'#type' => 'details',
'#title' => 'Load more',
];
$form['settings']['load_more']['on'] = [
'#type' => 'checkbox',
'#title' => 'Load more',
'#default_value' => \Drupal::config('comment_ajax_pager.settings')
->get('settings.' . $entityTypeId . '.' . $bundle . '.' . $field_name . '.on'),
];
$form['settings']['load_more']['text'] = [
'#type' => 'textfield',
'#field_prefix' => 't(',
'#field_suffix' => ')',
'#states' => [
'visible' => [
'input[name="settings[load_more][on]"]' => [
'checked' => TRUE,
],
],
],
'#description' => '@per_page@ - ' . $form['settings']['per_page']['#title'],
'#default_value' => \Drupal::config('comment_ajax_pager.settings')
->get('settings.' . $entityTypeId . '.' . $bundle . '.' . $field_name . '.text'),
];
$form['actions']['submit']['#submit'][] = 'comment_ajax_pager_settings_submit';
}
break;
}
}
function comment_ajax_pager_settings_submit($form, $form_state) {
if (!empty($form['#entity'])) {
$values = $form_state
->getValues();
$entity = $form['#entity'];
$entityTypeId = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
$field_name = $form['default_value']['widget']['#field_name'];
$config = \Drupal::configFactory()
->getEditable('comment_ajax_pager.settings');
$OldSettings = \Drupal::config('comment_ajax_pager.settings')
->get('settings');
if (empty($OldSettings)) {
$OldSettings = [];
}
$OldSettings[$entityTypeId][$bundle][$field_name] = $values['settings']['load_more'];
$config
->set('settings', $OldSettings);
$config
->save();
}
}