View source
<?php
namespace Drupal\content_calendar\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\node\Entity\NodeType;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
class SettingsForm extends ConfigFormBase {
protected $contentTypeConfigService;
protected $config;
const CONFIG_NAME = 'content_calendar.settings';
const DEFAULT_BG_COLOR_UNPUBLISHED_CONTENT = '#fff4f4';
public function __construct(ConfigFactoryInterface $config_factory) {
parent::__construct($config_factory);
$this->contentTypeConfigService = \Drupal::service('content_calendar.content_type_config_service');
$this->config = $this
->config(self::CONFIG_NAME);
}
public function getFormId() {
return 'content_calendar_settings';
}
protected function getEditableConfigNames() {
return [
'content_calendar.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$content_type_options = $this
->getConfiguredContentTypes();
if (!$content_type_options) {
$message = $this
->t('Content Calendar can only be used with Scheduler. At least one Content Type needs to have the scheduling options enabled.');
$this
->messenger()
->addMessage($message, 'error');
return [];
}
$this
->buildContentTypeConfiguration($form, $form_state);
$this
->buildCalendarOptions($form, $form_state);
$build_form = parent::buildForm($form, $form_state);
return $build_form;
}
protected function getConfiguredContentTypes() {
$display_options = [];
$node_types = NodeType::loadMultiple();
foreach ($node_types as $node_type_key => $node_type) {
if ($scheduler = $node_type
->getThirdPartySettings('scheduler')) {
if ($scheduler['publish_enable'] == TRUE) {
$display_options[$node_type_key] = $node_type
->label();
}
}
}
return $display_options;
}
protected function buildContentTypeConfiguration(array &$form, FormStateInterface $form_state) {
$content_type_options = $this
->getConfiguredContentTypes();
$entities = $this->contentTypeConfigService
->loadAllEntities();
$entity_keys = array_keys($entities);
$form['content_type_configuration'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Content Type Configuration'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
];
$form['content_type_configuration']['content_types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Which Content Types need to be displayed?'),
'#description' => $this
->t('Only Content Types enabled for Scheduler are listed here'),
'#required' => TRUE,
'#options' => $content_type_options,
'#default_value' => $entity_keys,
];
if ($entities) {
$rows = [];
foreach ($entities as $entity_key => $entity) {
$options = [
'query' => [
'destination' => Url::fromRoute('content_calendar.settings')
->toString(),
],
];
$edit_link = Link::createFromRoute($this
->t('Configure'), 'entity.content_type_config.edit_form', [
'content_type_config' => $entity_key,
], $options);
$row = [
$entity
->label(),
$entity
->id(),
$entity
->getColor(),
$edit_link
->toString(),
];
$rows[] = $row;
}
$headers = [
$this
->t('Content Type'),
$this
->t('ID'),
$this
->t('Color'),
$this
->t('Actions'),
];
$form['content_type_configuration']['table'] = [
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#weight' => 20,
];
}
}
protected function buildCalendarOptions(array &$form, FormStateInterface $form_state) {
$form['options'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Display Options'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
];
$user_picture_field_exists = !$this
->config('field.field.user.user.user_picture')
->isNew();
$form['options']['show_user_thumb'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show thumbnail image of User image'),
'#description' => $this
->t('This option is only available, if the User account has the "user_picture" field. See Account configuration.'),
'#disabled' => !$user_picture_field_exists,
'#default_value' => $this->config
->get('show_user_thumb'),
];
$form['options']['bg_color_unpublished_content'] = [
'#type' => 'textfield',
'#title' => $this
->t('Background color for unpublished content'),
'#description' => $this
->t("Define the background color for unpublished content. Use a css color in word format (e.x. red) or a hexadecimal value (e.x. #ffcc00). When empty the default value will be used."),
'#maxlength' => 20,
'#default_value' => $this->config
->get('bg_color_unpublished_content'),
];
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$this
->config(self::CONFIG_NAME)
->set('show_user_thumb', $values['show_user_thumb'])
->set('bg_color_unpublished_content', $values['bg_color_unpublished_content'])
->save();
$selected_content_types = $this
->getSelectedContentTypes($form_state);
$config_entities = $this->contentTypeConfigService
->loadAllEntities();
$this
->addNewConfigEntities($selected_content_types, $config_entities);
$this
->deleteObsoleteConfigEntities($selected_content_types, $config_entities);
}
protected function getSelectedContentTypes(FormStateInterface &$form_state) {
$values = $form_state
->getValues();
$selected_content_types = [];
foreach ($values['content_types'] as $key => $selected) {
if ($selected) {
$selected_content_types[] = $key;
}
}
return $selected_content_types;
}
protected function addNewConfigEntities(array $selected_content_types, array &$config_entities) {
$entity_keys = array_keys($config_entities);
foreach ($selected_content_types as $selected_content_type) {
if (!in_array($selected_content_type, $entity_keys)) {
if ($node_type = NodeType::load($selected_content_type)) {
$this->contentTypeConfigService
->createEntity($selected_content_type, $node_type
->label());
$this
->messenger()
->addMessage(t('Content Type @name has been added and can be configured below.', [
'@name' => $node_type
->label(),
]));
}
}
}
}
protected function deleteObsoleteConfigEntities(array $selected_content_types, array &$config_entities) {
foreach ($config_entities as $config_entity_id => $config_entity) {
if (!in_array($config_entity_id, $selected_content_types)) {
$this
->messenger()
->addMessage(t('Content Type @name has been removed from Content Calendar.', [
'@name' => $config_entity
->label(),
]));
$config_entity
->delete();
}
}
}
}