Commit a1b4df89 authored by amateescu's avatar amateescu
Browse files

Fix and update the XML Sitemap custom module.

parent d76d9e50
......@@ -2,10 +2,8 @@
namespace Drupal\xmlsitemap_custom\Controller;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Url;
/**
......@@ -13,34 +11,6 @@ use Drupal\Core\Url;
*/
class XmlSitemapCustomListController extends ControllerBase {
/**
* The language manager.
*
* @var \Drupal\language\ConfigurableLanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new XmlSitemapController object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
*/
public function __construct(ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager) {
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'), $container->get('language_manager')
);
}
/**
* Renders a list with all custom links.
*
......@@ -62,7 +32,6 @@ class XmlSitemapCustomListController extends ControllerBase {
);
$rows = array();
$destination = drupal_get_destination();
$query = db_select('xmlsitemap');
$query->fields('xmlsitemap');
......@@ -72,34 +41,26 @@ class XmlSitemapCustomListController extends ControllerBase {
$result = $query->execute();
foreach ($result as $link) {
$language = $this->languageManager->getLanguage($link->language);
$language = $this->languageManager()->getLanguage($link->language);
$row = array();
$row['loc'] = $this->l($link->loc, Url::fromUri($link->loc));
$row['loc'] = $this->l($link->loc, Url::fromUri('base://' . $link->loc));
$row['priority'] = number_format($link->priority, 1);
$row['changefreq'] = $link->changefreq ? drupal_ucfirst(xmlsitemap_get_changefreq($link->changefreq)) : t('None');
$row['changefreq'] = $link->changefreq ? Unicode::ucfirst(xmlsitemap_get_changefreq($link->changefreq)) : t('None');
if (isset($header['language'])) {
$row['language'] = t($language->name);
$row['language'] = $language->getName();
}
$operations['edit'] = array(
'title' => t('Edit'),
'route_name' => 'xmlsitemap_custom.edit',
'route_parameters' => array(
'link' => $link->id,
),
'url' => Url::fromRoute('xmlsitemap_custom.edit', ['link' => $link->id]),
);
$operations['delete'] = array(
'title' => t('Delete'),
'route_name' => 'xmlsitemap_custom.delete',
'route_parameters' => array(
'link' => $link->id,
),
'url' => Url::fromRoute('xmlsitemap_custom.delete', ['link' => $link->id]),
);
$row['operations'] = array(
'data' => array(
'#type' => 'operations',
'#theme' => 'links',
'#links' => $operations,
'#attributes' => array('class' => array('links', 'inline')),
),
);
$rows[] = $row;
......@@ -107,13 +68,12 @@ class XmlSitemapCustomListController extends ControllerBase {
// @todo Convert to tableselect
$build['xmlsitemap_custom_table'] = array(
'#type' => 'tableselect',
'#theme' => 'table',
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this->t('No custom links available. <a href="@custom_link">Add custom link</a>', array('@custom_link' => Url::fromRoute('xmlsitemap_custom.add', [], array('query' => $destination)))),
'#empty' => $this->t('No custom links available. <a href="@custom_link">Add custom link</a>', array('@custom_link' => Url::fromRoute('xmlsitemap_custom.add', [], array('query' => $this->getDestinationArray()))->toString())),
);
$build['xmlsitemap_custom_pager'] = array('#theme' => 'pager');
$build['xmlsitemap_custom_pager'] = array('#type' => 'pager');
return $build;
}
......
......@@ -2,14 +2,14 @@
namespace Drupal\xmlsitemap_custom\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Language\LanguageInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\xmlsitemap\XmlSitemapLinkStorageInterface;
......@@ -17,7 +17,14 @@ use Drupal\xmlsitemap\XmlSitemapLinkStorageInterface;
/**
* Provides a form for adding a custom link.
*/
class XmlSitemapCustomAddForm extends ConfigFormBase {
class XmlSitemapCustomAddForm extends FormBase {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* The language manager.
......@@ -43,8 +50,8 @@ class XmlSitemapCustomAddForm extends ConfigFormBase {
/**
* Constructs a new XmlSitemapCustomAddForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
* @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
......@@ -52,8 +59,8 @@ class XmlSitemapCustomAddForm extends ConfigFormBase {
* @param \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface $link_storage
* The xmlsitemap link storage service.
*/
public function __construct(ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, AliasManagerInterface $alias_manager, XmlSitemapLinkStorageInterface $link_storage) {
parent::__construct($config_factory);
public function __construct(Connection $connection, LanguageManagerInterface $language_manager, AliasManagerInterface $alias_manager, XmlSitemapLinkStorageInterface $link_storage) {
$this->connection = $connection;
$this->languageManager = $language_manager;
$this->aliasManager = $alias_manager;
$this->linkStorage = $link_storage;
......@@ -64,7 +71,10 @@ class XmlSitemapCustomAddForm extends ConfigFormBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'), $container->get('language_manager'), $container->get('path.alias_manager'), $container->get('xmlsitemap.link_storage')
$container->get('database'),
$container->get('language_manager'),
$container->get('path.alias_manager'),
$container->get('xmlsitemap.link_storage')
);
}
......@@ -79,11 +89,10 @@ class XmlSitemapCustomAddForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$query = db_select('xmlsitemap', 'x');
$query = $this->connection->select('xmlsitemap', 'x');
$query->addExpression('MAX(id)');
$id = $query->execute()->fetchField();
$link = array();
$link += array(
$link = array(
'id' => $id + 1,
'loc' => '',
'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
......@@ -97,43 +106,41 @@ class XmlSitemapCustomAddForm extends ConfigFormBase {
'#type' => 'value',
'#value' => 'custom',
);
$form['subtype'] = array(
'#type' => 'value',
'#value' => '',
);
$form['id'] = array(
'#type' => 'value',
'#value' => $link['id'],
);
$form['loc'] = array(
'#type' => 'textfield',
'#title' => t('Path to link'),
'#title' => $this->t('Path to link'),
'#field_prefix' => Url::fromRoute('<front>', [], array('absolute' => TRUE)),
'#default_value' => $link['loc'] ? drupal_get_path_alias($link['loc'], $link['language']) : '',
'#default_value' => $link['loc'] ? $this->aliasManager->getPathByAlias($link['loc'], $link['language']) : '',
'#required' => TRUE,
'#size' => 30,
);
$form['priority'] = array(
'#type' => 'select',
'#title' => t('Priority'),
'#title' => $this->t('Priority'),
'#options' => xmlsitemap_get_priority_options(),
'#default_value' => number_format($link['priority'], 1),
'#description' => t('The priority of this URL relative to other URLs on your site.'),
'#description' => $this->t('The priority of this URL relative to other URLs on your site.'),
);
$form['changefreq'] = array(
'#type' => 'select',
'#title' => t('Change frequency'),
'#options' => array(0 => t('None')) + xmlsitemap_get_changefreq_options(),
'#title' => $this->t('Change frequency'),
'#options' => array(0 => $this->t('None')) + xmlsitemap_get_changefreq_options(),
'#default_value' => $link['changefreq'],
'#description' => t('How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page.'),
'#description' => $this->t('How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page.'),
);
$languages = $this->languageManager->getLanguages();
$languages_list = array();
foreach ($languages as $key => $value) {
$languages_list[$key] = $value->getName();
}
$form['language'] = array(
'#type' => 'select',
'#title' => t('Language'),
'#type' => 'language_select',
'#title' => $this->t('Language'),
'#languages' => LanguageInterface::STATE_ALL,
'#default_value' => $link['language'],
'#options' => array(LanguageInterface::LANGCODE_NOT_SPECIFIED => t('Language neutral')) + $languages_list,
'#access' => $languages_list,
);
$form['actions'] = array(
......@@ -141,14 +148,16 @@ class XmlSitemapCustomAddForm extends ConfigFormBase {
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#value' => $this->t('Save'),
'#weight' => 5,
'#button_type' => 'primary',
);
$cancel_link = Url::fromRoute('xmlsitemap_custom.list');
$form['actions']['cancel'] = array(
'#markup' => $this->l(t('Cancel'), $cancel_link),
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this->t('Cancel'),
'#url' => Url::fromRoute('xmlsitemap_custom.list'),
'#weight' => 10,
);
];
return $form;
}
......@@ -163,7 +172,8 @@ class XmlSitemapCustomAddForm extends ConfigFormBase {
$link['loc'] = trim($link['loc']);
$link['loc'] = $this->aliasManager->getPathByAlias($link['loc'], $link['language']);
$form_state->setValue('loc', $link['loc']);
$query = db_select('xmlsitemap');
$query = $this->connection->select('xmlsitemap');
$query->fields('xmlsitemap');
$query->condition('type', 'custom');
$query->condition('loc', $link['loc']);
......@@ -171,15 +181,16 @@ class XmlSitemapCustomAddForm extends ConfigFormBase {
$query->condition('access', 1);
$query->condition('language', $link['language']);
$result = $query->execute()->fetchAssoc();
if ($result != FALSE) {
$form_state->setErrorByName('loc', t('There is already an existing link in the sitemap with the path %link.', array('%link' => $link['loc'])));
$form_state->setErrorByName('loc', $this->t('There is already an existing link in the sitemap with the path %link.', array('%link' => $link['loc'])));
}
try {
$client = new Client();
$res = $client->get(Url::fromRoute('<front>', [], array('absolute' => TRUE)) . $link['loc']);
$client->get(Url::fromRoute('<front>', [], array('absolute' => TRUE))->toString() . $link['loc']);
}
catch (ClientException $e) {
$form_state->setErrorByName('loc', t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $link['loc'])));
$form_state->setErrorByName('loc', $this->t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $link['loc'])));
}
parent::validateForm($form, $form_state);
......@@ -189,18 +200,12 @@ class XmlSitemapCustomAddForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->cleanValues();
$link = $form_state->getValues();
$this->linkStorage->save($link);
drupal_set_message(t('The custom link for %loc was saved.', array('%loc' => $link['loc'])));
drupal_set_message($this->t('The custom link for %loc was saved.', array('%loc' => $link['loc'])));
$form_state->setRedirect('xmlsitemap_custom.list');
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['xmlsitemap.link_storage'];
}
}
......@@ -4,15 +4,23 @@ namespace Drupal\xmlsitemap_custom\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Url;
use Drupal\xmlsitemap\XmlSitemapLinkStorage;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\xmlsitemap\XmlSitemapLinkStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Provides a form for deleting a custom link.
*/
class XmlSitemapCustomDeleteForm extends ConfirmFormBase {
/**
* The xmlsitemap link storage handler.
*
* @var \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface
*/
protected $linkStorage;
/**
* The path of the custom link.
*
......@@ -20,6 +28,25 @@ class XmlSitemapCustomDeleteForm extends ConfirmFormBase {
*/
protected $custom_link;
/**
* Constructs a new XmlSitemapCustomEditForm object.
*
* @param \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface $link_storage
* The xmlsitemap link storage service.
*/
public function __construct(XmlSitemapLinkStorageInterface $link_storage) {
$this->linkStorage = $link_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('xmlsitemap.link_storage')
);
}
/**
* {@inheritdoc}
*/
......@@ -31,18 +58,11 @@ class XmlSitemapCustomDeleteForm extends ConfirmFormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $link = '') {
$query = db_select('xmlsitemap');
$query->fields('xmlsitemap');
$query->condition('type', 'custom');
$query->condition('id', $link);
$result = $query->execute();
$link = $result->fetchAssoc();
if (!$link) {
drupal_set_message(t('No valid custom link specified.'), 'error');
return new RedirectResponse('/admin/config/search/xmlsitemap/custom/');
if (!$custom_link = $this->linkStorage->load('custom', $link)) {
throw new NotFoundHttpException();
}
else {
$this->custom_link = $link;
$this->custom_link = $custom_link;
}
return parent::buildForm($form, $form_state);
}
......@@ -51,7 +71,7 @@ class XmlSitemapCustomDeleteForm extends ConfirmFormBase {
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Delete');
return $this->t('Delete');
}
/**
......@@ -65,19 +85,19 @@ class XmlSitemapCustomDeleteForm extends ConfirmFormBase {
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to delete %link?', array('%link' => $this->custom_link['loc']));
return $this->t('Are you sure you want to delete %link?', array('%link' => $this->custom_link['loc']));
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
XmlSitemapLinkStorage::linkDelete('custom', $this->custom_link['id']);
drupal_set_message(t('The custom link for %loc has been deleted.', array('%loc' => $this->custom_link['loc'])));
$message = t('The custom link for %loc has been deleted.', array('%loc' => $this->custom_link['loc']));
\Drupal::logger('xmlsitemap')->debug($message);
$this->linkStorage->delete('custom', $this->custom_link['id']);
$message = $this->t('The custom link for %loc has been deleted.', array('%loc' => $this->custom_link['loc']));
$this->logger('xmlsitemap')->debug($message);
drupal_set_message($message);
$form_state->setRedirect('xmlsitemap_custom.list');
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
......@@ -2,14 +2,13 @@
namespace Drupal\xmlsitemap_custom\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Language\LanguageInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\xmlsitemap\XmlSitemapLinkStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
......@@ -17,7 +16,7 @@ use Drupal\Core\Url;
/**
* Provides a form for editing a custom link.
*/
class XmlSitemapCustomEditForm extends ConfigFormBase {
class XmlSitemapCustomEditForm extends FormBase {
/**
* The path of the custom link.
......@@ -48,10 +47,8 @@ class XmlSitemapCustomEditForm extends ConfigFormBase {
protected $linkStorage;
/**
* Constructs a new XmlSitemapCustomAddForm object.
* Constructs a new XmlSitemapCustomEditForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
* @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
......@@ -59,8 +56,7 @@ class XmlSitemapCustomEditForm extends ConfigFormBase {
* @param \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface $link_storage
* The xmlsitemap link storage service.
*/
public function __construct(ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, AliasManagerInterface $alias_manager, XmlSitemapLinkStorageInterface $link_storage) {
parent::__construct($config_factory);
public function __construct(LanguageManagerInterface $language_manager, AliasManagerInterface $alias_manager, XmlSitemapLinkStorageInterface $link_storage) {
$this->languageManager = $language_manager;
$this->aliasManager = $alias_manager;
$this->linkStorage = $link_storage;
......@@ -71,7 +67,9 @@ class XmlSitemapCustomEditForm extends ConfigFormBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'), $container->get('language_manager'), $container->get('path.alias_manager'), $container->get('xmlsitemap.link_storage')
$container->get('language_manager'),
$container->get('path.alias_manager'),
$container->get('xmlsitemap.link_storage')
);
}
......@@ -86,43 +84,29 @@ class XmlSitemapCustomEditForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $link = '') {
$query = db_select('xmlsitemap');
$query->fields('xmlsitemap');
$query->condition('type', 'custom');
$query->condition('id', $link);
$result = $query->execute();
$link = $result->fetchAssoc();
if (!$link) {
drupal_set_message(t('No valid custom link specified.'), 'error');
return new RedirectResponse('/admin/config/search/xmlsitemap/custom/');
if (!$custom_link = $this->linkStorage->load('custom', $link)) {
drupal_set_message($this->t('No valid custom link specified.'), 'error');
$this->redirect('xmlsitemap_custom.list');
}
else {
$this->custom_link = $link;
$this->custom_link = $custom_link;
}
$query = db_select('xmlsitemap', 'x');
$query->addExpression('MAX(id)');
$id = $query->execute()->fetchField();
$this->custom_link += array(
'id' => $id + 1,
'loc' => '',
'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
'lastmod' => 0,
'changefreq' => 0,
'changecount' => 0,
'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
);
$form['type'] = array(
'#type' => 'value',
'#value' => 'custom',
);
$form['subtype'] = array(
'#type' => 'value',
'#value' => '',
);
$form['id'] = array(
'#type' => 'value',
'#value' => $this->custom_link['id'],
);
$form['loc'] = array(
'#type' => 'textfield',
'#title' => t('Path to link'),
'#title' => $this->t('Path to link'),
'#field_prefix' => Url::fromRoute('<front>', [], array('absolute' => TRUE)),
'#default_value' => $this->custom_link['loc'],
'#required' => TRUE,
......@@ -130,29 +114,23 @@ class XmlSitemapCustomEditForm extends ConfigFormBase {
);
$form['priority'] = array(
'#type' => 'select',
'#title' => t('Priority'),
'#title' => $this->t('Priority'),
'#options' => xmlsitemap_get_priority_options(),
'#default_value' => number_format($this->custom_link['priority'], 1),
'#description' => t('The priority of this URL relative to other URLs on your site.'),
'#description' => $this->t('The priority of this URL relative to other URLs on your site.'),
);
$form['changefreq'] = array(
'#type' => 'select',
'#title' => t('Change frequency'),
'#options' => array(0 => t('None')) + xmlsitemap_get_changefreq_options(),
'#default_value' => $link['changefreq'],
'#description' => t('How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page.'),
'#title' => $this->t('Change frequency'),
'#options' => array(0 => $this->t('None')) + xmlsitemap_get_changefreq_options(),
'#default_value' => $this->custom_link['changefreq'],
'#description' => $this->t('How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page.'),
);
$languages = $this->languageManager->getLanguages();
$languages_list = array();
foreach ($languages as $key => $value) {
$languages_list[$key] = $value->getName();
}
$form['language'] = array(
'#type' => 'select',
'#title' => t('Language'),
'#type' => 'language_select',
'#title' => $this->t('Language'),
'#languages' => LanguageInterface::STATE_ALL,
'#default_value' => $this->custom_link['language'],
'#options' => array(LanguageInterface::LANGCODE_NOT_SPECIFIED => t('Language neutral')) + $languages_list,
'#access' => $languages_list,
);
$form['actions'] = array(
......@@ -160,14 +138,16 @@ class XmlSitemapCustomEditForm extends ConfigFormBase {
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#value' => $this->t('Save'),
'#weight' => 5,
'#button_type' => 'primary',
);
$cancel_link = Url::fromRoute('xmlsitemap_custom.list');
$form['actions']['cancel'] = array(
'#markup' => l(t('Cancel'), $cancel_link),
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this->t('Cancel'),
'#url' => Url::fromRoute('xmlsitemap_custom.list'),
'#weight' => 10,
);
];