View source
<?php
namespace Drupal\onlyone\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Url;
use Drupal\onlyone\Event\OnlyOneEvents;
use Drupal\onlyone\OnlyOneInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ConfigContentTypes extends ConfigFormBase {
protected $eventDispacher;
protected $onlyone;
public function __construct(ConfigFactoryInterface $config_factory, EventDispatcherInterface $event_dispacher, OnlyOneInterface $onlyone) {
parent::__construct($config_factory);
$this->eventDispacher = $event_dispacher;
$this->onlyone = $onlyone;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('event_dispatcher'), $container
->get('onlyone'));
}
public function getFormId() {
return 'onlyone_config_content_types';
}
protected function getEditableConfigNames() {
return [
'onlyone.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$available_content_types = $this->onlyone
->getAvailableContentTypesForPrint();
$cant_available_content_types = count($available_content_types);
if ($cant_available_content_types) {
$form['available_content_type'] = [
'#type' => 'details',
'#title' => $this
->t("Content types available to have Only One content"),
'#open' => TRUE,
];
$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.'),
];
}
$not_available_content_types = $this->onlyone
->getNotAvailableContentTypesForPrint();
$cant_not_available_content_types = count($not_available_content_types);
if ($cant_not_available_content_types) {
$collapsed = $cant_available_content_types ? FALSE : TRUE;
$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',
],
],
];
foreach ($not_available_content_types as $key => $value) {
$form['not_available_content_type'][$key] = [
'#type' => 'item',
'#markup' => $value,
];
}
$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(),
]),
];
}
if ($cant_available_content_types) {
return parent::buildForm($form, $form_state);
}
else {
$form = parent::buildForm($form, $form_state);
unset($form['actions']);
return $form;
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$content_types_checked = array_filter($form_state
->getValue('onlyone_node_types'));
$onlyone_content_types = $this
->config('onlyone.settings')
->get('onlyone_node_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 {
$this
->config('onlyone.settings')
->set('onlyone_node_types', $content_types_checked)
->save();
parent::submitForm($form, $form_state);
$this->eventDispacher
->dispatch(OnlyOneEvents::CONTENT_TYPES_UPDATED);
}
}
}