diff --git a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php index 17d9555..650eefc 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php @@ -17,6 +17,8 @@ */ interface EntityTypeInterface { + const BUNDLE_MAX_LENGTH = 32; + /** * Gets any arbitrary property. * diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index d9d86d4..f2fe09c 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -12,8 +12,12 @@ use Drupal\comment\CommentInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; +use Drupal\Component\Utility\String; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; +use Drupal\Core\Entity\EntityMalformedException; +use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Url; use Drupal\field\FieldInstanceConfigInterface; use Drupal\field\FieldConfigInterface; @@ -781,6 +785,26 @@ function comment_form_field_ui_field_overview_form_alter(&$form, $form_state) { if ($form['#entity_type'] == 'comment' && $request->attributes->has('commented_entity_type')) { $form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($request->attributes->get('commented_entity_type'), $request->attributes->get('field_name')); } + $form['#validate'][] = 'comment_field_ui_overview_form_validate'; +} + +/** + * Form validation callback for field_ui_field_overview_form(). + */ +function comment_field_ui_overview_form_validate(&$form, $form_state) { + $commented_entity_type_id = $form['#entity_type']; + $field_name = $form_state['values']['fields']['_add_new_field']['field_name']; + $type = $form_state['values']['fields']['_add_new_field']['field_name']['type']; + if ($type != 'comment') { + return; + } + $new_bundle = $commented_entity_type_id . '__' . $field_name; + if (Unicode::strlen($new_bundle) > EntityTypeInterface::BUNDLE_MAX_LENGTH) { + $this->setFormError('fields][_add_new_field][field_name', $form_state, $this->t('Add new field: The provided field name would exceed the maximum allowable length for a comment field on the %type entity type.', array( + '%type' => $commented_entity_type_id, + ))); + } + } /** @@ -902,6 +926,25 @@ function comment_entity_predelete(EntityInterface $entity) { } /** + * Implements hook_entity_presave(). + */ +function comment_entity_presave(EntityInterface $entity) { + if ($entity->getEntityTypeId() == 'field_config' && $entity->type == 'comment') { + $commented_entity_type = $entity->entity_type; + $field_name = $entity->name; + $bundle = $commented_entity_type . '__' . $field_name; + if (Unicode::strlen($bundle) > EntityTypeInterface::BUNDLE_MAX_LENGTH) { + throw new EntityMalformedException(t('You cannot create a comment field named %field_name on the %type Entity type, please select a shorter name such as %name', + array( + '%field_name' => $field_name, + '%type' => $commented_entity_type, + '%name' => substr($field_name, 0, EntityTypeInterface::BUNDLE_MAX_LENGTH - Unicode::strlen($bundle)), + ))); + } + } +} + +/** * Implements hook_node_update_index(). */ function comment_node_update_index(EntityInterface $node, $langcode) {