Commit e0e0dc4a authored by Ramon  Serra Vasconcelos's avatar Ramon Serra Vasconcelos Committed by Ahmad Abbad
Browse files

Issue #3305892 by pflora, ramonvasconcelos, Harsh panchal, Qusai Taha, Ahmad...

Issue #3305892 by pflora, ramonvasconcelos, Harsh panchal, Qusai Taha, Ahmad Abbad: Drupal calls should be avoided in classes, use dependency injection instead
parent 8fa1696f
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ CONTENTS OF THIS FILE
INTRODUCTION
------------

The module makes it easy to integrate with Gleab through the Gleap API key, 
The module makes it easy to integrate with Gleap through the Gleap API key,
this will allow the customer to give their feedback in an intelligent way.

 * For a full description of the module, visit the project page:
@@ -38,5 +38,5 @@ Visit https://www.drupal.org/node/1897420 for further information.
CONFIGURATION
-------------

- Get the Gleab API key from https://app.gleap.io/ > Project > Settings > Installation.
- To Add Gleab API key from /admin/config/services/gleab.
- Get the Gleap API key from https://app.gleap.io/ > Project > Settings > Installation.
- To Add Gleap API key from /admin/config/services/gleap.
+2 −2
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class of GleabConfiguration.
 * Class of GleapConfiguration.
 */
class GleapConfiguration extends ConfigFormBase {

@@ -32,7 +32,7 @@ class GleapConfiguration extends ConfigFormBase {
  public function buildForm(array $form, FormStateInterface $form_state) {
    // Default settings.
    $config = $this->config('gleap.gleap_configuration');
    // gleap API key.
    // Gleap API key.
    $form['gleap_api_key'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Gleap API key'),
+67 −6
Original line number Diff line number Diff line
@@ -8,6 +8,11 @@ use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;

/**
 * Provides a Gleap block.
@@ -17,24 +22,80 @@ use Drupal\Core\StringTranslation\StringTranslationTrait;
 * category = @Translation("Gleap")
 * )
 */
class GleapBlock extends BlockBase {
class GleapBlock extends BlockBase implements ContainerFactoryPluginInterface {

  use StringTranslationTrait;

  /**
   * The admin Gleap configuration.
   *
   * @var \Drupal\Core\Config\Config\ConfigFactoryInterface
   */
  protected ConfigFactoryInterface $configFactory;

  /**
   * The current user.
   *
   * @var Drupal\Core\Session\AccountInterface
   */
  protected AccountInterface $currentUser;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected MessengerInterface $messenger;

  /**
   * Gleap Block.
   *
   * @param array $configuration
   *   The Gleap block configuration.
   * @param mixed $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin path destination.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param Drupal\Core\Session\AccountInterface $currentUser
   *   The current user.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, AccountInterface $currentUser, MessengerInterface $messenger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $config_factory->get('gleap.gleap_configuration');
    $this->currentUser = $currentUser;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $config = \Drupal::config('gleap.gleap_configuration');
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('config.factory'),
      $container->get('current_user'),
      $container->get('messenger'),
    );
  }

    if (empty($config->get('gleap_api_key')) && (\Drupal::currentUser()->hasPermission('administer gleap'))) {
  /**
   * {@inheritdoc}
   */
  public function build(): array {
    if (empty($this->configFactory->get('gleap_api_key')) && ($this->currentUser->hasPermission('administer gleap'))) {
      $url = Url::fromUri('route:gleap_configuration');
      $link = new Link($this->t('here'), $url);
      $link = $link->toString()->getGeneratedLink();
      $script = Markup::create($this->t('Please fill out the Gleap API key from') . " " . $link);
      $script = Markup::create($this->t('Please fill out the Gleap API key from %link'), ['%link' => $link]);
    }
    else {
      $script = Markup::create('<script>!function(Gleap,t,i){if(!(Gleap=window.Gleap=window.Gleap||[]).invoked){for(window.GleapActions=[],Gleap.invoked=!0,Gleap.methods=["identify","clearIdentity","attachCustomData","setCustomData","removeCustomData","clearCustomData","registerCustomAction","logEvent","sendSilentCrashReport","startFeedbackFlow","setAppBuildNumber","setAppVersionCode","preFillForm","setApiUrl","setFrameUrl","isOpened","open","close","on","setLanguage","setOfflineMode","initialize"],Gleap.f=function(e){return function(){var t=Array.prototype.slice.call(arguments);window.GleapActions.push({e:e,a:t})}},t=0;t<Gleap.methods.length;t++)Gleap[i=Gleap.methods[t]]=Gleap.f(i);Gleap.load=function(){var t=document.getElementsByTagName("head")[0],i=document.createElement("script");i.type="text/javascript",i.async=!0,i.src="https://js.gleap.io/latest/index.js",t.appendChild(i)},Gleap.load(), Gleap.initialize("' . $config->get('gleap_api_key') . '") }}();</script>');
      $script = Markup::create('<script>!function(Gleap,t,i){if(!(Gleap=window.Gleap=window.Gleap||[]).invoked){for(window.GleapActions=[],Gleap.invoked=!0,Gleap.methods=["identify","clearIdentity","attachCustomData","setCustomData","removeCustomData","clearCustomData","registerCustomAction","logEvent","sendSilentCrashReport","startFeedbackFlow","setAppBuildNumber","setAppVersionCode","preFillForm","setApiUrl","setFrameUrl","isOpened","open","close","on","setLanguage","setOfflineMode","initialize"],Gleap.f=function(e){return function(){var t=Array.prototype.slice.call(arguments);window.GleapActions.push({e:e,a:t})}},t=0;t<Gleap.methods.length;t++)Gleap[i=Gleap.methods[t]]=Gleap.f(i);Gleap.load=function(){var t=document.getElementsByTagName("head")[0],i=document.createElement("script");i.type="text/javascript",i.async=!0,i.src="https://js.gleap.io/latest/index.js",t.appendChild(i)},Gleap.load(), Gleap.initialize("' . $this->configFactory->get('gleap_api_key') . '") }}();</script>');
    }
    return [
      '#markup' => $script,