helpers.inc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. use Drupal\Core\Form\FormStateInterface;
  3. /*
  4. * @file
  5. * Helper functions for the fieldgroup module.
  6. */
  7. /**
  8. * Get the default formatter settings for a given formatter and context.
  9. */
  10. function _field_group_get_default_formatter_settings($format_type, $context) {
  11. $manager = Drupal::service('plugin.manager.field_group.formatters');
  12. return $manager->getDefaultSettings($format_type, $context);
  13. }
  14. /**
  15. * Return an array of field_group_formatter options.
  16. */
  17. function field_group_field_formatter_options($type) {
  18. $options = &drupal_static(__FUNCTION__);
  19. if (!isset($options)) {
  20. $options = array();
  21. $manager = Drupal::service('plugin.manager.field_group.formatters');
  22. $formatters = $manager->getDefinitions();
  23. foreach ($formatters as $formatter) {
  24. if (in_array($type, $formatter['supported_contexts'])) {
  25. $options[$formatter['id']] = $formatter['label'];
  26. }
  27. }
  28. }
  29. return $options;
  30. }
  31. /**
  32. * Validate the entered css class from the submitted format settings.
  33. * @param Array $element The validated element
  34. * @param FormStateInterface $form_state The state of the form.
  35. */
  36. function field_group_validate_css_class($element, FormStateInterface $form_state) {
  37. $form_state_values = $form_state->getValues();
  38. $plugin_name = $form_state->get('plugin_settings_edit');
  39. if (!empty($form_state_values['fields'][$plugin_name]['settings_edit_form']['settings']['classes']) && !preg_match('!^[A-Za-z0-9-_ ]+$!', $form_state_values['fields'][$plugin_name]['settings_edit_form']['settings']['classes'])) {
  40. Drupal::formBuilder()->setError($element, $form_state, t('The css class must include only letters, numbers, underscores and dashes.'));
  41. }
  42. }
  43. /**
  44. * Validate the entered id attribute from the submitted format settings.
  45. * @param Array $element The validated element
  46. * @param FormStateInterface $form_state The state of the form.
  47. */
  48. function field_group_validate_id($element, FormStateInterface $form_state) {
  49. $form_state_values = $form_state->getValues();
  50. $plugin_name = $form_state->get('plugin_settings_edit');
  51. if (!empty($form_state_values['fields'][$plugin_name]['settings_edit_form']['settings']['id']) && !preg_match('!^[A-Za-z0-9-_]+$!', $form_state_values['fields'][$plugin_name]['settings_edit_form']['settings']['id'])) {
  52. Drupal::formBuilder()->setError($element, $form_state, t('The id must include only letters, numbers, underscores and dashes.'));
  53. }
  54. }