Skip to content

Instantly share code, notes, and snippets.

@mecmartini
Last active May 11, 2021 16:20
Show Gist options
  • Save mecmartini/a7b98d8cb46d6e2b712c3a7a14ad9b52 to your computer and use it in GitHub Desktop.
Save mecmartini/a7b98d8cb46d6e2b712c3a7a14ad9b52 to your computer and use it in GitHub Desktop.
fix-custom-body-class
diff --git a/custom_body_class.info.yml b/custom_body_class.info.yml
index 85b48ca..9c9683d 100644
--- a/custom_body_class.info.yml
+++ b/custom_body_class.info.yml
@@ -2,3 +2,4 @@ type: module
name : Custom body class
description : Adds custom class/es
core : 8.x
+core_version_requirement: ^8 || ^9
\ No newline at end of file
diff --git a/custom_body_class.module b/custom_body_class.module
index 51ac3ad..519c64d 100644
--- a/custom_body_class.module
+++ b/custom_body_class.module
@@ -44,9 +44,6 @@ function custom_body_class_form_alter(&$form, FormStateInterface &$form_state, $
'#type' => 'details',
'#title' => ('Custom Body Class Settings'),
'#group' => 'additional_settings',
- '#attached' => [
- 'library' => ['node_type_class/node_type_class.classes'],
- ],
];
$form['nodetype_body_class_group']['nodetype_class'] = [
'#title' => t('CSS class(es)'),
@@ -96,7 +93,7 @@ function custom_body_class_entity_base_field_info(EntityTypeInterface $entity_ty
function custom_body_class_preprocess_html(&$variables) {
$node = \Drupal::routeMatch()->getParameter('node');
- if ($node) {
+ if ($node && is_object($node)) {
$custom_body_class = $node->get('body_class')->getValue();
$specific_node_class = $node->get('specific_node_class')[0];
if (!empty($custom_body_class) && isset($custom_body_class[0]) && isset($custom_body_class[0]['value'])) {
@@ -105,7 +102,7 @@ function custom_body_class_preprocess_html(&$variables) {
if (!empty($specific_node_class) && ($specific_node_class->getValue() !== NULL) && ($specific_node_class->getValue()['value'] == 1)) {
$variables['attributes']['class'][] = $node->gettype();
}
- $type = \Drupal::service('entity.manager')->getStorage('node_type')->load($node->getType());
+ $type = \Drupal::entityTypeManager()->getStorage('node_type')->load($node->getType());
$classes = $type->getThirdPartySetting('node_type_class', 'classes', '');
$variables['attributes']['class'][] = $classes;
}
@@ -115,7 +112,7 @@ function custom_body_class_preprocess_html(&$variables) {
* Implements _node_special_character_form_validate().
*/
function _node_special_character_form_validate($form, $form_state) {
- if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬]/', $form_state->getValue('body_class')[0]['value'])) {
+ if ($form_state->getValue('body_class') && preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬]/', $form_state->getValue('body_class')[0]['value'] )) {
$form_state->setErrorByName('body_class', 'Special character in Custom Body Class');
}
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬]/', $form_state->getValue('nodetype_class'))) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment