diff --git a/config/schema/like.schema.yml b/config/schema/like.schema.yml index 850b06e53a0fdbb38fd170343a74cd4dba8bc56a..1994564c229e05a27b5d06f81d08331eb3106feb 100644 --- a/config/schema/like.schema.yml +++ b/config/schema/like.schema.yml @@ -7,9 +7,15 @@ like.settings: label: 'Enabled entity types' sequence: type: string + like_text: + type: string + label: 'Like button text' + icon_class: + type: string + label: 'CSS class for like button icon' like_cookie_expiry_time: type: integer - label: 'Like Cookie Expiry Time' + label: 'Like Cookie expiry time in seconds' cache_type: type: string label: 'Cache Type' diff --git a/src/Form/LikeForm.php b/src/Form/LikeForm.php index 529c28b10915d38fc335df38ae174a6c3b9aaf9c..597eca73557711fcd3a9b5e887f040e6c2d2dae8 100644 --- a/src/Form/LikeForm.php +++ b/src/Form/LikeForm.php @@ -9,6 +9,8 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\like\LikeHelperInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Render\Markup; /** * Like form class. @@ -29,6 +31,13 @@ class LikeForm extends FormBase { */ protected LikeHelperInterface $likeHelper; + /** + * The config factory service. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + /** * Constructs a LikeForm object. * @@ -36,10 +45,13 @@ class LikeForm extends FormBase { * The current user. * @param \Drupal\like\LikeHelperInterface $like_helper * The like helper service. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory service. */ - public function __construct(AccountInterface $current_user, LikeHelperInterface $like_helper) { + public function __construct(AccountInterface $current_user, LikeHelperInterface $like_helper, ConfigFactoryInterface $config_factory) { $this->currentUser = $current_user; $this->likeHelper = $like_helper; + $this->configFactory = $config_factory; } /** @@ -48,7 +60,8 @@ class LikeForm extends FormBase { public static function create(ContainerInterface $container) { return new static( $container->get('current_user'), - $container->get('like.helper') + $container->get('like.helper'), + $container->get('config.factory') ); } @@ -64,6 +77,7 @@ class LikeForm extends FormBase { */ public function buildForm(array $form, FormStateInterface $form_state, FieldItemInterface $item = NULL, array $settings = []) { $entity = $item->getEntity(); + $config = $this->config('like.settings'); $form['#item'] = $item; $form['#theme'] = 'like_form'; $form['#id'] = Html::getUniqueId('like-form-' . $entity->getEntityTypeId() . '-' . $entity->id()); @@ -94,13 +108,15 @@ class LikeForm extends FormBase { 'progress' => ['type' => 'none'], ], ]; + $form['label']['icon'] = [ '#type' => 'html_tag', '#tag' => 'i', '#attributes' => [ - 'class' => ['fa-solid', 'fa-heart'], + 'class' => $config->get('icon_class', ['fa-solid', 'fa-heart']), ], ]; + $form['label']['txt'] = [ '#type' => 'html_tag', '#tag' => 'span', @@ -178,7 +194,9 @@ class LikeForm extends FormBase { * Returns the likes text for the item. */ protected function likesTxt(int $value): string { - return $this->t('(<span>@value</span>) Likes', ['@value' => $value]); + $likes = $this->config('like.settings')->get('like_text'); + $likes_text = !empty($likes) ? $likes : 'Likes'; + return Markup::create('(<span>' . $value . '</span>) ' . $likes_text); } } diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index 6b616d12bcef96a33bdc60b77e12f01a6e4c5d3b..c3ff900f199e971ecb1cda7bb18113d295e3cdb2 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -99,6 +99,20 @@ class SettingsForm extends ConfigFormBase { '#default_value' => $config->get('like_cookie_expiry_time'), ]; + $form['like_text'] = [ + '#type' => 'textfield', + '#title' => $this->t('Like Text'), + '#description' => $this->t('Please enter the text to display'), + '#default_value' => $config->get('like_text') ? t($config->get('like_text')) : t('Likes'), + ]; + + $form['icon_class'] = [ + '#type' => 'textfield', + '#title' => $this->t('FA Icons'), + '#description' => $this->t('Please enter FA Icon class to display'), + '#default_value' => $config->get('icon_class'), + ]; + $form['cache_type'] = [ '#type' => 'radios', '#title' => $this->t('Cache type'), @@ -120,6 +134,8 @@ class SettingsForm extends ConfigFormBase { parent::submitForm($form, $form_state); $config = $this->config('like.settings'); $config->set('enabled_entity_types', $form_state->getValue('enabled_entity_types')); + $config->set('like_text', $form_state->getValue('like_text')); + $config->set('icon_class', $form_state->getValue('icon_class')); $config->set('like_cookie_expiry_time', (int) $form_state->getValue('like_cookie_expiry_time')); $config->set('cache_type', $form_state->getValue('cache_type')); $config->save();