You are here

function social_core_field_widget_form_alter in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()
  2. 8 modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()
  3. 8.2 modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()
  4. 8.3 modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()
  5. 8.4 modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()
  6. 8.5 modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()
  7. 8.6 modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()
  8. 8.7 modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()
  9. 10.3.x modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()
  10. 10.0.x modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()
  11. 10.1.x modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()
  12. 10.2.x modules/social_features/social_core/social_core.module \social_core_field_widget_form_alter()

Implements hook_field_widget_form_alter().

File

modules/social_features/social_core/social_core.module, line 508
The Social core module.

Code

function social_core_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  $field_definition = $context['items']
    ->getFieldDefinition();
  if ($field_definition
    ->getType() == 'path') {

    // Set the correct title.
    $element['#title'] = t('URL path settings');
    $url = Url::fromRoute('<front>', [], [
      'absolute' => TRUE,
    ]);

    // Protocols we can strip.
    $protocols = [
      'https://www.',
      'http://www.',
      'https://',
      'http://',
      'www.',
    ];

    // The URL.
    $truncate_url = $url
      ->toString();

    // Loop over the protocols and try to replace them in the string.
    foreach ($protocols as $protocol) {
      $truncate_url = str_replace($protocol, '', $truncate_url);
    }

    // Put the protocoless, truncated URL in the addon.
    $element['alias']['#field_prefix'] = '<div class="input-group input-group-expanded"><span class="input-group-addon">' . $truncate_url . '</span>';
    $element['alias']['#field_suffix'] = '</div>';
    $element['alias']['#description'] = t('The URL alias allows you to customise the link to this page.');

    // URLs are stored with a leading slash but our display includes that slash
    // in the prefix so we remove it here.
    if (isset($element['alias']['#default_value'])) {
      $element['alias']['#default_value'] = ltrim($element['alias']['#default_value'], '/');
    }
    $element['#element_validate'] = [
      'social_core_path_validate',
    ];
  }
}