class ConfigContentTypes in Allow a content type only once (Only One) 8
Class ConfigContentTypes.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\onlyone\Form\ConfigContentTypes
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of ConfigContentTypes
1 string reference to 'ConfigContentTypes'
File
- src/
Form/ ConfigContentTypes.php, line 17
Namespace
Drupal\onlyone\FormView source
class ConfigContentTypes extends ConfigFormBase {
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispacher;
/**
* The onlyone service.
*
* @var \Drupal\onlyone\OnlyOneInterface
*/
protected $onlyone;
/**
* Constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispacher
* The event dispacher.
* @param \Drupal\onlyone\OnlyOneInterface $onlyone
* The onlyone service.
*/
public function __construct(ConfigFactoryInterface $config_factory, EventDispatcherInterface $event_dispacher, OnlyOneInterface $onlyone) {
parent::__construct($config_factory);
$this->eventDispacher = $event_dispacher;
$this->onlyone = $onlyone;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('event_dispatcher'), $container
->get('onlyone'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'onlyone_config_content_types';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'onlyone.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Getting the available content types.
$available_content_types = $this->onlyone
->getAvailableContentTypesForPrint();
// Getting the number of content types.
$cant_available_content_types = count($available_content_types);
if ($cant_available_content_types) {
// The details form element with the available content types.
$form['available_content_type'] = [
'#type' => 'details',
'#title' => $this
->t("Content types available to have Only One content"),
'#open' => TRUE,
];
// All the available content types.
$form['available_content_type']['onlyone_node_types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Configure these content types to have Only One content per language:'),
'#options' => $available_content_types,
'#default_value' => $this
->config('onlyone.settings')
->get('onlyone_node_types'),
'#description' => $this
->t('The selected content types will allow Only One content per language.'),
];
}
// Getting the non-available content types.
$not_available_content_types = $this->onlyone
->getNotAvailableContentTypesForPrint();
// Getting the number of not availables content types.
$cant_not_available_content_types = count($not_available_content_types);
// If all the content types are available we don't need to show the element.
if ($cant_not_available_content_types) {
$collapsed = $cant_available_content_types ? FALSE : TRUE;
// The details form element with the unavailable content types.
$form['not_available_content_type'] = [
'#type' => 'details',
'#title' => $this
->t('Content types not available to have Only One content per language'),
'#description' => $this
->t('Content types which have more than one content in at least one language:'),
'#open' => $collapsed,
'#attributes' => [
'class' => [
'details-description--not-available-content-types',
],
],
];
// Showing all the not availables content types.
foreach ($not_available_content_types as $key => $value) {
$form['not_available_content_type'][$key] = [
'#type' => 'item',
'#markup' => $value,
];
}
// Attaching the css file.
$form['#attached']['library'] = [
'onlyone/admin_settings',
];
}
if (!$cant_available_content_types && !$cant_not_available_content_types) {
$form['not_available_content_type'] = [
'#markup' => $this
->t('There are not content types on this site, go to the <a href=":add-content-type">Add content type</a> page to create one.', [
':add-content-type' => Url::fromRoute('node.type_add')
->toString(),
]),
];
}
// Show the submit button if there is availables content types.
if ($cant_available_content_types) {
return parent::buildForm($form, $form_state);
}
else {
$form = parent::buildForm($form, $form_state);
unset($form['actions']);
return $form;
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Cleaning the not checked content types from the selected checkboxes.
$content_types_checked = array_filter($form_state
->getValue('onlyone_node_types'));
// Getting the configured content types.
$onlyone_content_types = $this
->config('onlyone.settings')
->get('onlyone_node_types');
// Checking if we have any change in the configured content types.
if ($content_types_checked == array_values($onlyone_content_types)) {
$this
->messenger()
->addWarning($this
->t("You don't have changed the configured content types."));
}
else {
// Saving the configuration.
$this
->config('onlyone.settings')
->set('onlyone_node_types', $content_types_checked)
->save();
// Calling parent method.
parent::submitForm($form, $form_state);
// Dispatching the event related to a change in the configured content
// types.
$this->eventDispacher
->dispatch(OnlyOneEvents::CONTENT_TYPES_UPDATED);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigContentTypes:: |
protected | property | The event dispatcher. | |
ConfigContentTypes:: |
protected | property | The onlyone service. | |
ConfigContentTypes:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
ConfigContentTypes:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
ConfigContentTypes:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
ConfigContentTypes:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ConfigContentTypes:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
ConfigContentTypes:: |
public | function |
Constructor. Overrides ConfigFormBase:: |
|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
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 | 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. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
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. |