Skip to content
Snippets Groups Projects
Commit b4fde764 authored by Viktor Holovachek's avatar Viktor Holovachek
Browse files

Issue #3421857 - Add default values

parent a5b2930d
No related branches found
No related tags found
3 merge requests!142.0.x,!131.0.x,!9Issue #3421857 - Add default values
Pipeline #96561 passed with warnings
......@@ -36,6 +36,7 @@ function entity_body_class_entity_base_field_info(EntityTypeInterface $entity_ty
if (in_array('Drupal\Core\Entity\ContentEntityInterface', class_implements($entity_type->getOriginalClass())) &&
$entity_type->getLinkTemplate('canonical')
) {
\Drupal::logger('module')->notice($entity_type->id());
$fields['entity_body_class'] = BaseFieldDefinition::create('string')
->setLabel(t('Body CSS class(es)'))
->setDescription(t('e.g. <em>my-body-class-1 my-body-class-2</em>'))
......@@ -59,6 +60,15 @@ function entity_body_class_form_alter(&$form, FormStateInterface $form_state, $f
if ($object instanceof ContentEntityFormInterface &&
array_key_exists('entity_body_class', $form)
) {
$config = \Drupal::configFactory()->getEditable('entity_body_class.fields');
$entity = $object->getEntity();
$types = $config->get('types');
if ($types && $entity->isNew() && !empty($types[$entity->getEntityTypeId()])) {
$widget = &$form['entity_body_class']['widget'];
$widget[0]['value']['#default_value'] = $types[$entity->getEntityTypeId()];
}
$form['entity_body_class']['#access'] = \Drupal::currentUser()->hasPermission('access entity body class fields');
$form['#validate'][] = 'entity_body_class_entity_form_validate';
}
......
access entity body class settings:
title: 'Manage body class settings'
description: 'Allows a user to manage body class settings.'
access entity body class fields:
title: 'Manage body class fields'
description: 'Allows a user to manage body class fields.'
entity_body_class.settings:
path: '/admin/config/content/body-class-settings'
defaults:
_form: '\Drupal\entity_body_class\Form\EntityBodyClassForm'
_title: 'Manage entity body class settings'
requirements:
_permission: 'access entity body class settings'
<?php
namespace Drupal\entity_body_class\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form handler for adding body class settings.
*/
class EntityBodyClassForm extends ConfigFormBase {
/**
* Returns the entity_type.manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a EntityBodyClassForm form.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Provides an interface for entity type managers.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($config_factory);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'entity_body_class';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'entity_body_class.fields',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->configFactory->getEditable('entity_body_class.fields');
$form['types'] = [
'#type' => 'fieldset',
'#title' => $this->t('Default body classes'),
'#tree' => TRUE,
];
foreach ($this->entityTypeManager->getDefinitions() as $definition) {
if (in_array('Drupal\Core\Entity\ContentEntityInterface', class_implements($definition->getOriginalClass())) &&
$definition->getLinkTemplate('canonical')
) {
$id = $definition->id();
$form['types'][$id] = [
'#type' => 'textfield',
'#title' => $definition->getLabel()->render(),
'#default_value' => isset($config->get('types')[$id]) ? $config->get('types')[$id] : '',
];
}
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configFactory->getEditable('entity_body_class.fields')
->set('types', array_filter($form_state->getValue('types')))
->save();
parent::submitForm($form, $form_state);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment