Commit 36384f16 authored by beagaliana's avatar beagaliana
Browse files

Issue #3259598 by beagaliana: Inject the service for the key module and cache tags

parent c11127e8
Loading
Loading
Loading
Loading
+5 −7
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ class WhatsappSettingsForm extends ConfigFormBase {
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'id_account';
    return 'whatsapp_settings';
  }

  /**
@@ -49,10 +49,10 @@ class WhatsappSettingsForm extends ConfigFormBase {
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config(static::SETTINGS);

    $form['id_account'] = [
      '#type' => 'textfield',
    $form['widget_key'] = [
      '#type' => 'key_select',
      '#title' => $this->t('Widget key'),
      '#default_value' => $config->get('id_account'),
      '#default_value' => $config->get('widget_key'),
    ];

    return parent::buildForm($form, $form_state);
@@ -62,10 +62,8 @@ class WhatsappSettingsForm extends ConfigFormBase {
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Retrieve the configuration.
    $this->configFactory->getEditable(static::SETTINGS)
      // Set the submitted configuration setting.
      ->set('id_account', $form_state->getValue('id_account'))
      ->set('widget_key', $form_state->getValue('widget_key'))
      ->save();

    parent::submitForm($form, $form_state);
+26 −17
Original line number Diff line number Diff line
@@ -5,10 +5,10 @@ namespace Drupal\whatsapp\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\key\KeyRepository;
use Drupal\key\Key;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a 'WhatsApp' Block.
@@ -27,6 +27,13 @@ class WhatsappBlock extends BlockBase implements ContainerFactoryPluginInterface
     */
    protected $configFactory;

    /**
     * The key.
     *
     * @var \Drupal\key\KeyRepository
     */
    protected $configKey;

    /**
     * Constructs a new WhatsappBlock object.
     *
@@ -38,10 +45,13 @@ class WhatsappBlock extends BlockBase implements ContainerFactoryPluginInterface
     *   The plugin implementation definition.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The factory for configuration objects.
     * @param \Drupal\key\KeyRepository $config_key
     *   The key.
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
    public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, KeyRepository $config_key) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->configFactory = $config_factory;
        $this->configKey = $config_key;
    }

    /**
@@ -52,7 +62,8 @@ class WhatsappBlock extends BlockBase implements ContainerFactoryPluginInterface
            $configuration,
            $plugin_id,
            $plugin_definition,
            $container->get('config.factory')
            $container->get('config.factory'),
            $container->get('key.repository')
        );
    }

@@ -60,16 +71,14 @@ class WhatsappBlock extends BlockBase implements ContainerFactoryPluginInterface
     * {@inheritdoc}
     */
    public function build() {
        $config_key = $this->configFactory->get('whatsapp.settings')->get('id_account');
        echo $config_key;

        $iframe_url = "https://widget.tochat.be/bundle.js?key=" . $config_key;
      $widget_key = $this->configFactory->get('whatsapp.settings')->get('widget_key');
      $key = $this->configKey->getKey($widget_key)->getKeyValue();

      return [
        '#type' => 'inline_template',
        '#template' => '<script defer src="{{ url }}"></script>',
        '#context' => [
            'url' => $iframe_url,
          'url' => "//widget.tochat.be/bundle.js?key=$key",
        ],
      ];

@@ -78,8 +87,8 @@ class WhatsappBlock extends BlockBase implements ContainerFactoryPluginInterface
    /**
     * {@inheritdoc}
     */
    public function getCacheContexts() {
        return Cache::mergeContexts(parent::getCacheContexts(), ['languages:language_interface']);
    public function getCacheTags() {
        return Cache::mergeTags(['config:whatsapp.settings']);
    }

}