From 6c548c064b96982061cec324083b49e763eaf10d Mon Sep 17 00:00:00 2001 From: Sahana N <sahana.sn90@gmail.com> Date: Wed, 29 Jan 2025 11:29:08 +0530 Subject: [PATCH 01/10] Added the configuration for likes text and icon. --- src/Form/LikeForm.php | 37 ++++++++++++++++++++++------- src/Form/SettingsForm.php | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/Form/LikeForm.php b/src/Form/LikeForm.php index 529c28b..d9edf8a 100644 --- a/src/Form/LikeForm.php +++ b/src/Form/LikeForm.php @@ -9,6 +9,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\like\LikeHelperInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\file\Entity\File; /** * Like form class. @@ -94,13 +95,31 @@ class LikeForm extends FormBase { 'progress' => ['type' => 'none'], ], ]; - $form['label']['icon'] = [ - '#type' => 'html_tag', - '#tag' => 'i', - '#attributes' => [ - 'class' => ['fa-solid', 'fa-heart'], - ], - ]; + $icon = \Drupal::config('like.settings')->get('image'); + $file = File::load($icon); + $image_url = $file ? $file->createFileUrl() : ''; + dump($image_url); + if($file){ + $form['label']['image'] = [ + '#type' => 'html_tag', + '#tag' => 'img', + '#attributes' => [ + 'src' => $image_url, + 'alt' => 'Uploaded Image', + 'class' => ['custom-image-class'], + ], + ]; + } + else{ + $form['label']['icon'] = [ + '#type' => 'html_tag', + '#tag' => 'i', + '#attributes' => [ + 'class' => ['fa-solid', 'fa-heart'], + ], + ]; + } + $form['label']['txt'] = [ '#type' => 'html_tag', '#tag' => 'span', @@ -178,7 +197,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 = \Drupal::config('like.settings')->get('like_text'); + $likes_text = !empty($likes) ? $likes : 'Likes'; + return $this->t('(<span>@value</span>) @likes', ['@value' => $value, '@likes' => $likes_text]); } } diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index 6b616d1..9551cb3 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\file\Entity\File; /** * Configures like button settings. @@ -99,6 +100,40 @@ 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'), + ]; + + + if($config->get('image')){ + $file = File::load($config->get('image')); + $form['like_icon'] = [ + '#type' => 'managed_file', + '#title' => $this->t('Like Icon'), + '#upload_location' => 'public://custom_images/', + '#upload_validators' => [ + 'file_validate_extensions' => ['jpg jpeg png gif'], + 'file_validate_size' => [2 * 1024 * 1024], // 2 MB limit + ], + '#default_value' => [ + 'target_id' => $file->id(), + ], + ]; + } + else { + $form['like_icon'] = [ + '#type' => 'managed_file', + '#title' => $this->t('Like Icon'), + '#upload_location' => 'public://custom_images/', + '#upload_validators' => [ + 'file_validate_extensions' => ['jpg jpeg png gif'], + 'file_validate_size' => [2 * 1024 * 1024], // 2 MB limit + ], + ]; + } $form['cache_type'] = [ '#type' => 'radios', '#title' => $this->t('Cache type'), @@ -118,8 +153,23 @@ class SettingsForm extends ConfigFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); + $image = $form_state->getValue('image'); + if (!empty($image)) { + // Save the file as permanent. + $file = \Drupal::entityTypeManager()->getStorage('file')->load($image[0]); + if ($file) { + $file->setPermanent(); + $file->save(); + } + } $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')); + if($file) + { + $config->set('image', $file->id()); + } + $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(); -- GitLab From a9a7166dbf6348a2acebfc9ed1a6189575ee8e9b Mon Sep 17 00:00:00 2001 From: Sahana N <sahana.sn90@gmail.com> Date: Mon, 10 Feb 2025 13:26:38 +0530 Subject: [PATCH 02/10] Updated the code. --- src/Form/LikeForm.php | 13 ++++------- src/Form/SettingsForm.php | 46 ++++++--------------------------------- 2 files changed, 11 insertions(+), 48 deletions(-) diff --git a/src/Form/LikeForm.php b/src/Form/LikeForm.php index d9edf8a..507ca0f 100644 --- a/src/Form/LikeForm.php +++ b/src/Form/LikeForm.php @@ -95,18 +95,13 @@ class LikeForm extends FormBase { 'progress' => ['type' => 'none'], ], ]; - $icon = \Drupal::config('like.settings')->get('image'); - $file = File::load($icon); - $image_url = $file ? $file->createFileUrl() : ''; - dump($image_url); - if($file){ + $icon = \Drupal::config('like.settings')->get('icon_class'); + if($icon) { $form['label']['image'] = [ '#type' => 'html_tag', - '#tag' => 'img', + '#tag' => 'i', '#attributes' => [ - 'src' => $image_url, - 'alt' => 'Uploaded Image', - 'class' => ['custom-image-class'], + 'class' => [$icon], ], ]; } diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index 9551cb3..b23ec77 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -108,32 +108,13 @@ class SettingsForm extends ConfigFormBase { ]; - if($config->get('image')){ - $file = File::load($config->get('image')); - $form['like_icon'] = [ - '#type' => 'managed_file', - '#title' => $this->t('Like Icon'), - '#upload_location' => 'public://custom_images/', - '#upload_validators' => [ - 'file_validate_extensions' => ['jpg jpeg png gif'], - 'file_validate_size' => [2 * 1024 * 1024], // 2 MB limit - ], - '#default_value' => [ - 'target_id' => $file->id(), - ], - ]; - } - else { - $form['like_icon'] = [ - '#type' => 'managed_file', - '#title' => $this->t('Like Icon'), - '#upload_location' => 'public://custom_images/', - '#upload_validators' => [ - 'file_validate_extensions' => ['jpg jpeg png gif'], - 'file_validate_size' => [2 * 1024 * 1024], // 2 MB limit - ], + $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'), @@ -153,23 +134,10 @@ class SettingsForm extends ConfigFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); - $image = $form_state->getValue('image'); - if (!empty($image)) { - // Save the file as permanent. - $file = \Drupal::entityTypeManager()->getStorage('file')->load($image[0]); - if ($file) { - $file->setPermanent(); - $file->save(); - } - } $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')); - if($file) - { - $config->set('image', $file->id()); - } - + $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(); -- GitLab From 32e7319455911e87205318974c294552a61c1af5 Mon Sep 17 00:00:00 2001 From: PremSuthar <jangidprem1999@gmail.com> Date: Mon, 10 Feb 2025 19:48:05 +0530 Subject: [PATCH 03/10] Resolve the Suggestions. --- config/schema/like.schema.yml | 8 +++++++- src/Form/LikeForm.php | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/config/schema/like.schema.yml b/config/schema/like.schema.yml index 850b06e..cb1698c 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: '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 507ca0f..89c4ede 100644 --- a/src/Form/LikeForm.php +++ b/src/Form/LikeForm.php @@ -10,6 +10,7 @@ use Drupal\Core\Session\AccountInterface; use Drupal\like\LikeHelperInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\file\Entity\File; +use Drupal\Core\Render\Markup; /** * Like form class. @@ -194,7 +195,7 @@ class LikeForm extends FormBase { protected function likesTxt(int $value): string { $likes = \Drupal::config('like.settings')->get('like_text'); $likes_text = !empty($likes) ? $likes : 'Likes'; - return $this->t('(<span>@value</span>) @likes', ['@value' => $value, '@likes' => $likes_text]); + return Markup::create('(<span>' . $value . '</span>) ' . $likes_text); } } -- GitLab From 16c7e8b9a0b3cb679db481d45a28f0f37323d90d Mon Sep 17 00:00:00 2001 From: PremSuthar <jangidprem1999@gmail.com> Date: Mon, 10 Feb 2025 19:52:08 +0530 Subject: [PATCH 04/10] removed unwanted use statments. --- src/Form/LikeForm.php | 1 - src/Form/SettingsForm.php | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/Form/LikeForm.php b/src/Form/LikeForm.php index 89c4ede..2a92bc8 100644 --- a/src/Form/LikeForm.php +++ b/src/Form/LikeForm.php @@ -9,7 +9,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\like\LikeHelperInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\file\Entity\File; use Drupal\Core\Render\Markup; /** diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index b23ec77..e6d237e 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -8,7 +8,6 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\file\Entity\File; /** * Configures like button settings. @@ -107,7 +106,6 @@ class SettingsForm extends ConfigFormBase { '#default_value' => $config->get('like_text'), ]; - $form['icon_class'] = [ '#type' => 'textfield', '#title' => $this->t('FA Icons'), -- GitLab From 221f640b67bd12292972a7c6b775ecff5aacf194 Mon Sep 17 00:00:00 2001 From: PremSuthar <jangidprem1999@gmail.com> Date: Mon, 10 Feb 2025 19:54:57 +0530 Subject: [PATCH 05/10] Added teh use statment. --- src/Form/LikeForm.php | 1 + src/Form/SettingsForm.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Form/LikeForm.php b/src/Form/LikeForm.php index 2a92bc8..89c4ede 100644 --- a/src/Form/LikeForm.php +++ b/src/Form/LikeForm.php @@ -9,6 +9,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\like\LikeHelperInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\file\Entity\File; use Drupal\Core\Render\Markup; /** diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index e6d237e..c72adef 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\file\Entity\File; /** * Configures like button settings. -- GitLab From c8f59edc51cd2f55e640710f765d7a10db991eab Mon Sep 17 00:00:00 2001 From: PremSuthar <jangidprem1999@gmail.com> Date: Mon, 10 Feb 2025 20:26:36 +0530 Subject: [PATCH 06/10] removed unwanted use statments. --- src/Form/LikeForm.php | 1 - src/Form/SettingsForm.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Form/LikeForm.php b/src/Form/LikeForm.php index 89c4ede..2a92bc8 100644 --- a/src/Form/LikeForm.php +++ b/src/Form/LikeForm.php @@ -9,7 +9,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\like\LikeHelperInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\file\Entity\File; use Drupal\Core\Render\Markup; /** diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index c72adef..e6d237e 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -8,7 +8,6 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\file\Entity\File; /** * Configures like button settings. -- GitLab From c78d01f0c876f6045a2edf3288a12c883b7d695c Mon Sep 17 00:00:00 2001 From: PremSuthar <jangidprem1999@gmail.com> Date: Tue, 11 Feb 2025 13:27:12 +0530 Subject: [PATCH 07/10] Resolved the suggestions. --- config/schema/like.schema.yml | 2 +- src/Form/LikeForm.php | 46 ++++++++++++++++++----------------- src/Form/SettingsForm.php | 2 +- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/config/schema/like.schema.yml b/config/schema/like.schema.yml index cb1698c..1994564 100644 --- a/config/schema/like.schema.yml +++ b/config/schema/like.schema.yml @@ -15,7 +15,7 @@ like.settings: label: 'CSS class for like button icon' like_cookie_expiry_time: type: integer - label: 'Cookie expiry time in seconds' + 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 2a92bc8..8d1d91e 100644 --- a/src/Form/LikeForm.php +++ b/src/Form/LikeForm.php @@ -9,6 +9,7 @@ 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; /** @@ -30,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. * @@ -37,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; } /** @@ -49,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') ); } @@ -65,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()); @@ -95,25 +108,14 @@ class LikeForm extends FormBase { 'progress' => ['type' => 'none'], ], ]; - $icon = \Drupal::config('like.settings')->get('icon_class'); - if($icon) { - $form['label']['image'] = [ - '#type' => 'html_tag', - '#tag' => 'i', - '#attributes' => [ - 'class' => [$icon], - ], - ]; - } - else{ - $form['label']['icon'] = [ - '#type' => 'html_tag', - '#tag' => 'i', - '#attributes' => [ - 'class' => ['fa-solid', 'fa-heart'], - ], - ]; - } + $icon = $config->get('icon_class'); + $form['label']['icon'] = [ + '#type' => 'html_tag', + '#tag' => 'i', + '#attributes' => [ + 'class' => explode(' ', $icon), + ], + ]; $form['label']['txt'] = [ '#type' => 'html_tag', @@ -192,7 +194,7 @@ class LikeForm extends FormBase { * Returns the likes text for the item. */ protected function likesTxt(int $value): string { - $likes = \Drupal::config('like.settings')->get('like_text'); + $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 e6d237e..9db178c 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -103,7 +103,7 @@ class SettingsForm extends ConfigFormBase { '#type' => 'textfield', '#title' => $this->t('Like Text'), '#description' => $this->t('Please enter the text to display'), - '#default_value' => $config->get('like_text'), + '#default_value' => $config->get('like_text') ?? 'Like', ]; $form['icon_class'] = [ -- GitLab From 47dad85ce401df284e84911785f3f973058965f7 Mon Sep 17 00:00:00 2001 From: PremSuthar <jangidprem1999@gmail.com> Date: Tue, 11 Feb 2025 14:01:40 +0530 Subject: [PATCH 08/10] Update the condition and add the default values. --- src/Form/LikeForm.php | 2 +- src/Form/SettingsForm.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Form/LikeForm.php b/src/Form/LikeForm.php index 8d1d91e..3254a4d 100644 --- a/src/Form/LikeForm.php +++ b/src/Form/LikeForm.php @@ -108,7 +108,7 @@ class LikeForm extends FormBase { 'progress' => ['type' => 'none'], ], ]; - $icon = $config->get('icon_class'); + $icon = $config->get('icon_class') ?: 'fa-solid fa-heart'; $form['label']['icon'] = [ '#type' => 'html_tag', '#tag' => 'i', diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index 9db178c..1934688 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -103,7 +103,7 @@ class SettingsForm extends ConfigFormBase { '#type' => 'textfield', '#title' => $this->t('Like Text'), '#description' => $this->t('Please enter the text to display'), - '#default_value' => $config->get('like_text') ?? 'Like', + '#default_value' => $config->get('icon_class') ?: 'Likes', ]; $form['icon_class'] = [ -- GitLab From aa6777892d8a0b9e235b0e4df1571148cec306cf Mon Sep 17 00:00:00 2001 From: PremSuthar <jangidprem1999@gmail.com> Date: Tue, 11 Feb 2025 15:32:27 +0530 Subject: [PATCH 09/10] Resolved the suggestions. --- src/Form/LikeForm.php | 4 ++-- src/Form/SettingsForm.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Form/LikeForm.php b/src/Form/LikeForm.php index 3254a4d..419e175 100644 --- a/src/Form/LikeForm.php +++ b/src/Form/LikeForm.php @@ -108,12 +108,12 @@ class LikeForm extends FormBase { 'progress' => ['type' => 'none'], ], ]; - $icon = $config->get('icon_class') ?: 'fa-solid fa-heart'; + $form['label']['icon'] = [ '#type' => 'html_tag', '#tag' => 'i', '#attributes' => [ - 'class' => explode(' ', $icon), + 'class' => $config->get('icon_class') ?: 'fa-solid fa-heart', ], ]; diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index 1934688..c3ff900 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -103,7 +103,7 @@ class SettingsForm extends ConfigFormBase { '#type' => 'textfield', '#title' => $this->t('Like Text'), '#description' => $this->t('Please enter the text to display'), - '#default_value' => $config->get('icon_class') ?: 'Likes', + '#default_value' => $config->get('like_text') ? t($config->get('like_text')) : t('Likes'), ]; $form['icon_class'] = [ -- GitLab From d24e342d82c0a71feb28b0f7bc7772e1952bd348 Mon Sep 17 00:00:00 2001 From: PremSuthar <jangidprem1999@gmail.com> Date: Tue, 11 Feb 2025 16:32:32 +0530 Subject: [PATCH 10/10] Resolved the suggestions. --- src/Form/LikeForm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Form/LikeForm.php b/src/Form/LikeForm.php index 419e175..597eca7 100644 --- a/src/Form/LikeForm.php +++ b/src/Form/LikeForm.php @@ -113,7 +113,7 @@ class LikeForm extends FormBase { '#type' => 'html_tag', '#tag' => 'i', '#attributes' => [ - 'class' => $config->get('icon_class') ?: 'fa-solid fa-heart', + 'class' => $config->get('icon_class', ['fa-solid', 'fa-heart']), ], ]; -- GitLab