Commit 40ef03a3 authored by beagaliana's avatar beagaliana
Browse files

Issue #3259598 by beagaliana: Add a Whatsapp block that includes the chatwithio javascript

parent 2a070bb5
Loading
Loading
Loading
Loading
+73 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Contains \Drupal\whatsapp\Form\WhatsappSettingsForm.
 */

namespace Drupal\whatsapp\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Configure WhatsApp module settings.
 */
class WhatsappSettingsForm extends ConfigFormBase {

  /**
   * Config settings.
   *
   * @var string
   */
  const SETTINGS = 'whatsapp.settings';

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'id_account';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      static::SETTINGS,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config(static::SETTINGS);

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

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@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'))
      ->save();

    parent::submitForm($form, $form_state);
  }
}
+85 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\whatsapp\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a 'WhatsApp' Block.
 *
 * @Block(
 *   id = "whatsapp_block",
 *   admin_label = @Translation("WhatsApp block"),
 * )
 */
class WhatsappBlock extends BlockBase implements ContainerFactoryPluginInterface {

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

    /**
     * Constructs a new WhatsappBlock object.
     *
     * @param array $configuration
     *   A configuration array containing information about the plugin instance.
     * @param string $plugin_id
     *   The plugin_id for the plugin instance.
     * @param mixed $plugin_definition
     *   The plugin implementation definition.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The factory for configuration objects.
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->configFactory = $config_factory;
    }

    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static(
            $configuration,
            $plugin_id,
            $plugin_definition,
            $container->get('config.factory')
        );
    }

    /**
     * {@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;

        return [
          '#type' => 'inline_template',
          '#template' => '<script defer src="{{ url }}"></script>',
          '#context' => [
            'url' => $iframe_url,
          ],
        ];

    }

    /**
     * {@inheritdoc}
     */
    public function getCacheContexts() {
        return Cache::mergeContexts(parent::getCacheContexts(), ['languages:language_interface']);
    }

}

whatsapp.routing.yml

0 → 100644
+7 −0
Original line number Diff line number Diff line
whatsapp.settings_form:
  path: '/admin/config/services/whatsapp'
  defaults:
    _form: '\Drupal\whatsapp\Form\WhatsappSettingsForm'
    _title: 'WhatsApp Settings'
  requirements:
    _permission: 'whatsapp configuration form'