Skip to content
Snippets Groups Projects
Commit 0c4be583 authored by Anna Demianik's avatar Anna Demianik
Browse files

#3492278: Added module settings; moved credentials to configuration

parent 18a8349f
No related branches found
No related tags found
1 merge request!6#3492278: Added module settings; moved credentials to configuration
Pipeline #365660 passed
<?php
/**
* @file
* Install, update and uninstall functions for the bing_indexing_api module.
*/
declare(strict_types=1);
/**
* Implements hook_update_N().
*
* Updated configuration for sites that installed the module before.
*/
function bing_indexing_api_update_10001(): void {
$state = \Drupal::state();
if ($api_key = $state->get('bing_index_api_key')) {
\Drupal::configFactory()->getEditable('bing_indexing_api.settings')
->set('node_presave', TRUE)
->set('node_presave_published_only', TRUE)
->set('node_unpublish', TRUE)
->set('node_delete', TRUE)
->set('node_delete_published_only', TRUE)
->save();
\Drupal::configFactory()->getEditable('bing_indexing_api.credentials')
->set('api_key', $api_key)
->set('base_domain', $state->get('bing_index_api_base_domain') ?? '')
->save();
$state->delete('bing_index_api_base_domain');
$state->delete('bing_index_api_key');
}
}
bing_indexing_api.settings_form:
route_name: bing_indexing_api.settings_form
title: 'Bing Index API'
description: 'Update URLs via the Bing Indexing API.'
bing_indexing_api.credentials_form:
route_name: bing_indexing_api.credentials_form
title: 'Bing Indexing API'
description: 'Bing Indexing API settings.'
parent: system.admin_config_services
bing_indexing_api.credentials_form:
route_name: bing_indexing_api.credentials_form
base_route: bing_indexing_api.credentials_form
title: 'Credentials'
weight: 1
bing_indexing_api.settings_form:
route_name: bing_indexing_api.settings_form
base_route: bing_indexing_api.settings_form
base_route: bing_indexing_api.credentials_form
title: 'Settings'
weight: 1
weight: 2
bing_indexing_api.bing_index_api_bulk_update_form:
route_name: bing_indexing_api.bing_index_api_bulk_update_form
base_route: bing_indexing_api.settings_form
bing_indexing_api.bulk_update_form:
route_name: bing_indexing_api.bulk_update_form
base_route: bing_indexing_api.credentials_form
title: 'Bulk Update'
weight: 2
\ No newline at end of file
weight: 3
......@@ -7,6 +7,7 @@
declare(strict_types=1);
use Drupal\bing_indexing_api\Form\BingSettingsForm;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
......@@ -30,26 +31,47 @@ function bing_indexing_api_help(string $route_name, RouteMatchInterface $route_m
* Implements hook_node_predelete().
*/
function bing_indexing_api_node_predelete(NodeInterface $node): void {
// Update the URL only if deleting a published node.
if ($node->isPublished()) {
$configuration = \Drupal::config(BingSettingsForm::CONFIG_NAME);
if ($configuration->get('node_delete')) {
// Skip if unpublished node.
if ($configuration->get('node_delete_published_only') && !$node->isPublished()) {
return;
}
\Drupal::service('bing_indexing_api.client')->reindexUrl($node->toUrl()
->setAbsolute()
->toString());
}
}
/**
* Implements hook_entity_ENTITY_TYPE_presave().
*/
function bing_indexing_api_node_presave(NodeInterface $node): void {
$original = !empty($node->original) ? $node->original : NULL;
if ($original && $node->isDefaultRevision()) {
// Trigger URL reindexing only if the node status changes from
// published to unpublished.
if ($original->isPublished() && !$node->isPublished()) {
\Drupal::service('bing_indexing_api.client')->reindexUrl($node->toUrl()
->toString());
}
if (!$node->isDefaultRevision()) {
return;
}
// Define whether we should trigger URL submission based on settings
// and the current node status.
$configuration = \Drupal::config(BingSettingsForm::CONFIG_NAME);
if ($configuration->get('node_presave') && $configuration->get('node_presave_published_only')) {
$should_trigger = $node->isPublished();
}
elseif ($configuration->get('node_presave')) {
$should_trigger = TRUE;
}
elseif ($configuration->get('node_unpublish')) {
$original = !empty($node->original) ? $node->original : NULL;
$should_trigger = $original && $original->isPublished() && !$node->isPublished();
}
if ($should_trigger ?? FALSE) {
\Drupal::service('bing_indexing_api.client')->reindexUrl($node->toUrl()
->setAbsolute()
->toString());
}
}
bing_indexing_api.credentials_form:
path: '/admin/config/services/bing-index-api'
defaults:
_form: '\Drupal\bing_indexing_api\Form\BingCredentialsForm'
_title: 'Credentials | Bing Indexing API'
requirements:
_permission: 'administer bing index api'
bing_indexing_api.settings_form:
path: '/admin/config/services/bing-index-api'
path: '/admin/config/services/bing-index-api/settings'
defaults:
_form: '\Drupal\bing_indexing_api\Form\BingSettingsForm'
_title: 'Bing Indexing API'
_title: 'Settings | Bing Indexing API'
requirements:
_permission: 'administer bing index api'
bing_indexing_api.bing_index_api_bulk_update_form:
bing_indexing_api.bulk_update_form:
path: '/admin/config/services/bing-index-api/bulk-update'
defaults:
_form: '\Drupal\bing_indexing_api\Form\BingBulkUpdateForm'
_title: 'Bing Indexing API - Bulk Update'
_title: 'Bulk Update | Bing Indexing API'
requirements:
_permission: 'administer bing index api'
services:
bing_indexing_api.client:
class: Drupal\bing_indexing_api\Service\BingIndexingApi
arguments: ['@state', '@logger.channel.bing_indexing_api', '@http_client']
arguments: ['@config.factory', '@logger.channel.bing_indexing_api', '@http_client']
logger.channel.bing_indexing_api:
parent: logger.channel_base
......
# Schema for the configuration files of the Bing Indexing API module.
bing_indexing_api.credentials:
type: config_object
label: 'Bing Indexing API credentials'
mapping:
base_domain:
type: string
label: 'The Base Domain'
api_key:
type: string
label: 'API key'
bing_indexing_api.settings:
type: config_object
label: 'Bing Indexing API settings'
mapping:
node_presave:
type: boolean
label: 'Track node creation/updating'
node_presave_published_only:
type: boolean
label: 'Only creation/updating of published nodes.'
node_unpublish:
type: boolean
label: 'Track node unpublishing'
node_delete:
type: boolean
label: 'Track node deletion'
node_delete_published_only:
type: boolean
label: 'Only the deletion of published nodes'
<?php
declare(strict_types=1);
namespace Drupal\bing_indexing_api\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* Provides Bing Credentials Form.
*/
class BingCredentialsForm extends ConfigFormBase {
const CONFIG_NAME = 'bing_indexing_api.credentials';
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'bing_index_api_credentials_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [self::CONFIG_NAME];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->config(self::CONFIG_NAME);
$form['base_domain'] = [
'#type' => 'textfield',
'#title' => $this->t('The Base Domain'),
'#description' => $this->t('Provide the site domain. Example: https://example.com'),
'#default_value' => $config->get('base_domain'),
'#required' => TRUE,
];
$form['api_key'] = [
'#type' => 'textfield',
'#title' => $this->t('API Key'),
'#default_value' => $config->get('api_key'),
'#description' => $this->t('Follow @instruction_link to configure Bing and get API key.', [
'@instruction_link' => Link::fromTextAndUrl($this->t('instruction'), Url::fromUri('https://learn.microsoft.com/en-us/bingwebmaster/getting-access#using-api-key'))
->toString(),
]),
'#required' => TRUE,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->config(self::CONFIG_NAME)
->set('base_domain', $form_state->getValue('base_domain'))
->set('api_key', $form_state->getValue('api_key'))
->save();
parent::submitForm($form, $form_state);
}
}
......@@ -4,75 +4,84 @@ declare(strict_types=1);
namespace Drupal\bing_indexing_api\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Url;
use Drupal\file\FileUsage\FileUsageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides Bing Settings Form.
*/
class BingSettingsForm extends FormBase {
class BingSettingsForm extends ConfigFormBase {
/**
* Constructs a new BingSettingsForm object.
*
* @param \Drupal\Core\State\StateInterface $state
* The State Service.
* @param \Drupal\file\FileUsage\FileUsageInterface $fileUsage
* The file usage service.
*/
public function __construct(protected StateInterface $state, protected FileUsageInterface $fileUsage) {
}
const CONFIG_NAME = 'bing_indexing_api.settings';
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new self(
$container->get('state'),
$container->get('file.usage')
);
protected function getEditableConfigNames() {
return [self::CONFIG_NAME];
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'bing_index_api_settings_form';
return 'bing_indexing_api_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$form['top'] = [
$config = $this->config(self::CONFIG_NAME);
$form['settings'] = [
'#type' => 'fieldset',
'#title' => $this->t('Settings'),
'#title' => $this->t('URL Submission Settings'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
];
$form['top']['bing_index_api_base_domain'] = [
'#type' => 'textfield',
'#title' => $this->t('The Base Domain of the site'),
'#description' => $this->t('Provide the site domain.'),
'#default_value' => $this->state->get('bing_index_api_base_domain'),
'#required' => TRUE,
$form['settings']['node_presave'] = [
'#type' => 'checkbox',
'#title' => $this->t('Track node creation/updating'),
'#description' => $this->t('Trigger URL submission when the node is created or updated.'),
'#default_value' => $config->get('node_presave'),
];
$form['settings']['node_presave_published_only'] = [
'#type' => 'checkbox',
'#title' => $this->t('Only creation/updating of published nodes.'),
'#description' => $this->t('Trigger URL submission when the node is created or updated, but only if the node is published. Recommended.'),
'#default_value' => $config->get('node_presave_published_only'),
'#states' => [
'visible' => [
':input[name="node_presave"]' => ['checked' => TRUE],
],
],
];
$form['top']['bing_index_api_key'] = [
'#type' => 'textfield',
'#title' => $this->t('API key'),
'#default_value' => $this->state->get('bing_index_api_key'),
'#description' => $this->t('Follow @instruction_link to configure Bing and get API key.', [
'@instruction_link' => Link::fromTextAndUrl($this->t('instruction'), Url::fromUri('https://learn.microsoft.com/en-us/bingwebmaster/getting-access#using-api-key'))
->toString(),
]),
'#required' => TRUE,
$form['settings']['node_unpublish'] = [
'#type' => 'checkbox',
'#title' => $this->t('Track node unpublishing'),
'#description' => $this->t('Trigger URL submission when the node becomes unpublished. Recommended.'),
'#default_value' => $config->get('node_unpublish'),
];
$form['settings']['node_delete'] = [
'#type' => 'checkbox',
'#title' => $this->t('Track node deletion'),
'#description' => $this->t('Trigger URL submission when the node is deleted.'),
'#default_value' => $config->get('node_delete'),
];
$form['settings']['node_delete_published_only'] = [
'#type' => 'checkbox',
'#title' => $this->t('Only the deletion of published nodes'),
'#description' => $this->t('Trigger URL submission only when the deleted node is published. Recommended.'),
'#default_value' => $config->get('node_presave_published_only'),
'#states' => [
'visible' => [
':input[name="node_delete"]' => ['checked' => TRUE],
],
],
];
$form['submit'] = [
......@@ -87,8 +96,15 @@ class BingSettingsForm extends FormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->state->set('bing_index_api_key', $form_state->getValue('bing_index_api_key'));
$this->state->set('bing_index_api_base_domain', $form_state->getValue('bing_index_api_base_domain'));
$this->config(self::CONFIG_NAME)
->set('node_presave', $form_state->getValue('node_presave'))
->set('node_presave_published_only', $form_state->getValue('node_presave_published_only'))
->set('node_unpublish', $form_state->getValue('node_unpublish'))
->set('node_delete', $form_state->getValue('node_delete'))
->set('node_delete_published_only', $form_state->getValue('node_delete_published_only'))
->save();
parent::submitForm($form, $form_state);
}
}
......@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Drupal\bing_indexing_api\Service;
use Drupal\Component\Serialization\Json;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
......@@ -27,14 +27,14 @@ class BingIndexingApi implements BingIndexingApiInterface {
/**
* Constructs BingIndexingApi.
*
* @param \Drupal\Core\State\StateInterface $state
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
* The State Service.
* @param \Psr\Log\LoggerInterface $logger
* The Logging Service.
* @param \GuzzleHttp\ClientInterface $client
* Http Client.
*/
public function __construct(protected StateInterface $state, protected LoggerInterface $logger, protected ClientInterface $client) {
public function __construct(protected ConfigFactoryInterface $config, protected LoggerInterface $logger, protected ClientInterface $client) {
}
/**
......@@ -80,8 +80,9 @@ class BingIndexingApi implements BingIndexingApiInterface {
if (empty($urls)) {
return NULL;
}
$base_domain = $this->state->get('bing_index_api_base_domain');
$api_key = $this->state->get('bing_index_api_key');
$config = $this->config->get('bing_indexing_api.credentials');
$base_domain = $config->get('base_domain');
$api_key = $config->get('api_key');
if (!$base_domain || !$api_key) {
$this->logger->error($this->t('Check the Bing API settings: the domain or API key is missing.'));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment