class XmlSitemapCustomAddForm in XML sitemap 8
Same name and namespace in other branches
- 2.x xmlsitemap_custom/src/Form/XmlSitemapCustomAddForm.php \Drupal\xmlsitemap_custom\Form\XmlSitemapCustomAddForm
Provides a form for adding a custom link.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\xmlsitemap_custom\Form\XmlSitemapCustomAddForm
Expanded class hierarchy of XmlSitemapCustomAddForm
1 string reference to 'XmlSitemapCustomAddForm'
- xmlsitemap_custom.routing.yml in xmlsitemap_custom/
xmlsitemap_custom.routing.yml - xmlsitemap_custom/xmlsitemap_custom.routing.yml
File
- xmlsitemap_custom/
src/ Form/ XmlSitemapCustomAddForm.php, line 19
Namespace
Drupal\xmlsitemap_custom\FormView source
class XmlSitemapCustomAddForm extends FormBase {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* The language manager.
*
* @var \Drupal\language\ConfigurableLanguageManagerInterface
*/
protected $languageManager;
/**
* The alias manager service.
*
* @var \Drupal\path_alias\AliasManagerInterface
*/
protected $aliasManager;
/**
* The HTTP client to fetch the feed data with.
*
* @var \Drupal\Core\Http\ClientFactory
*/
protected $httpClientFactory;
/**
* The xmlsitemap link storage handler.
*
* @var \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface
*/
protected $linkStorage;
/**
* Constructs a new XmlSitemapCustomAddForm object.
*
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
* @param \Drupal\path_alias\AliasManagerInterface $alias_manager
* The path alias manager service.
* @param \Drupal\Core\Http\ClientFactory $http_client_factory
* A Guzzle client object.
* @param \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface $link_storage
* The xmlsitemap link storage service.
*/
public function __construct(Connection $connection, LanguageManagerInterface $language_manager, AliasManagerInterface $alias_manager, ClientFactory $http_client_factory, XmlSitemapLinkStorageInterface $link_storage) {
$this->connection = $connection;
$this->languageManager = $language_manager;
$this->aliasManager = $alias_manager;
$this->httpClientFactory = $http_client_factory;
$this->linkStorage = $link_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('database'), $container
->get('language_manager'), $container
->get('path_alias.manager'), $container
->get('http_client_factory'), $container
->get('xmlsitemap.link_storage'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'xmlsitemap_custom_add';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Take into account that databases use wildly different names for their
// data types.
$db_type = $this->connection
->databaseType();
switch ($db_type) {
case 'mysql':
$type = 'UNSIGNED';
break;
case 'pgsql':
$type = 'BIGINT';
break;
case 'sqlite':
$type = 'INTEGER';
break;
default:
$type = 'INT';
break;
}
$query = $this->connection
->select('xmlsitemap', 'x');
$query
->addExpression("MAX(CAST(id AS {$type}))");
$query
->condition('type', 'custom');
$id = (int) $query
->execute()
->fetchField();
$link = [
'id' => $id + 1,
'loc' => '',
'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
'lastmod' => 0,
'changefreq' => 0,
'changecount' => 0,
'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
$form['type'] = [
'#type' => 'value',
'#value' => 'custom',
];
$form['subtype'] = [
'#type' => 'value',
'#value' => '',
];
$form['id'] = [
'#type' => 'value',
'#value' => $link['id'],
];
$form['loc'] = [
'#type' => 'textfield',
'#title' => $this
->t('Path to link'),
'#field_prefix' => rtrim(Url::fromRoute('<front>', [], [
'absolute' => TRUE,
])
->toString(), '/'),
'#default_value' => $link['loc'] ? $this->aliasManager
->getPathByAlias($link['loc'], $link['language']) : '',
'#description' => $this
->t('Use a relative path with a slash in front. For example, "/about".'),
'#required' => TRUE,
'#size' => 30,
];
$form['priority'] = [
'#type' => 'select',
'#title' => $this
->t('Priority'),
'#options' => xmlsitemap_get_priority_options(),
'#default_value' => number_format($link['priority'], 1),
'#description' => $this
->t('The priority of this URL relative to other URLs on your site.'),
];
$form['changefreq'] = [
'#type' => 'select',
'#title' => $this
->t('Change frequency'),
'#options' => [
0 => $this
->t('None'),
] + xmlsitemap_get_changefreq_options(),
'#default_value' => $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.'),
];
$form['language'] = [
'#type' => 'language_select',
'#title' => $this
->t('Language'),
'#languages' => LanguageInterface::STATE_ALL,
'#default_value' => $link['language'],
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#weight' => 5,
'#button_type' => 'primary',
];
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#url' => Url::fromRoute('xmlsitemap_custom.list'),
'#weight' => 10,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$link = $form_state
->getValues();
if (strpos($link['loc'], '/') !== 0) {
$form_state
->setErrorByName('loc', $this
->t('The path should start with /.'));
return;
}
// Make sure we trim and normalize the path first.
$link['loc'] = trim($link['loc']);
$link['loc'] = $this->aliasManager
->getPathByAlias($link['loc'], $link['language']);
$form_state
->setValue('loc', $link['loc']);
$query = $this->connection
->select('xmlsitemap');
$query
->fields('xmlsitemap');
$query
->condition('type', 'custom');
$query
->condition('loc', $link['loc']);
$query
->condition('status', 1);
$query
->condition('access', 1);
$query
->condition('language', $link['language']);
$result = $query
->execute()
->fetchAssoc();
if ($result != FALSE) {
$form_state
->setErrorByName('loc', $this
->t('There is already an existing link in the sitemap with the path %link.', [
'%link' => $link['loc'],
]));
}
try {
$client = $this->httpClientFactory
->fromOptions([
'config/curl',
[
CURLOPT_FOLLOWLOCATION => FALSE,
],
]);
$client
->get(Url::fromUserInput($link['loc'], [
'absolute' => TRUE,
])
->toString());
} catch (\Exception $e) {
$form_state
->setErrorByName('loc', $this
->t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', [
'@link' => $link['loc'],
]));
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->cleanValues();
$link = $form_state
->getValues();
$this->linkStorage
->save($link);
$this
->messenger()
->addStatus($this
->t('The custom link for %loc was saved.', [
'%loc' => $link['loc'],
]));
$form_state
->setRedirect('xmlsitemap_custom.list');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. | |
XmlSitemapCustomAddForm:: |
protected | property | The alias manager service. | |
XmlSitemapCustomAddForm:: |
protected | property | The database connection. | |
XmlSitemapCustomAddForm:: |
protected | property | The HTTP client to fetch the feed data with. | |
XmlSitemapCustomAddForm:: |
protected | property | The language manager. | |
XmlSitemapCustomAddForm:: |
protected | property | The xmlsitemap link storage handler. | |
XmlSitemapCustomAddForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
XmlSitemapCustomAddForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
XmlSitemapCustomAddForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
XmlSitemapCustomAddForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
XmlSitemapCustomAddForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
XmlSitemapCustomAddForm:: |
public | function | Constructs a new XmlSitemapCustomAddForm object. |