You are here

function name_element_render_component in Name Field 8

Helper function to render a component within a name element.

Parameters

array $components: Core properties for all components.

string $component_key: The component key of the component that is being rendered.

array $base_element: Base FAPI element that makes up a name element.

bool $core: Flag that indicates that the component is required as part of a valid name.

Return value

array The constructed component FAPI structure for a name element.

1 call to name_element_render_component()
name_element_expand in ./name.module
The #process callback to create the element.

File

./name.module, line 425
Defines an API for displaying and inputing names.

Code

function name_element_render_component(array $components, $component_key, array $base_element, $core) {
  $component = $components[$component_key];
  $element = [];

  // Allow other modules to append additional FAPI properties to the element.
  foreach (Element::properties($component) as $key) {
    $element[$key] = $component[$key];
  }
  $element['#attributes']['class'][] = 'name-element';
  $element['#attributes']['class'][] = 'name-' . $component_key;
  if ($core) {
    $element['#attributes']['class'][] = 'name-core-component';
  }
  if (isset($component['attributes'])) {
    foreach ($component['attributes'] as $key => $attribute) {
      if (isset($element['#attributes'][$key])) {
        if (is_array($attribute)) {
          $element['#attributes'][$key] = array_merge($element['#attributes'][$key], $attribute);
        }
        else {
          $element['#attributes'][$key] .= ' ' . $attribute;
        }
      }
      else {
        $element['#attributes'][$key] = $attribute;
      }
    }
  }
  $base_attributes = [
    'type',
    'title',
    'size',
    'maxlength',
  ];
  foreach ($base_attributes as $key) {
    $element['#' . $key] = $component[$key];
  }
  if (isset($base_element['#value'][$component_key])) {
    $element['#default_value'] = $base_element['#value'][$component_key];
  }
  if ($component['type'] == 'select') {
    $element['#options'] = $component['options'];
    $element['#size'] = 1;
  }
  elseif (!empty($component['autocomplete'])) {
    $element += $component['autocomplete'];
  }
  $show_component_required_marker = $core && !empty($base_element['#show_component_required_marker']) && !in_array('default_value_input', $base_element['#field_parents'], TRUE);

  // Enable the title options.
  $title_display = isset($component['title_display']) ? $component['title_display'] : 'description';
  switch ($title_display) {
    case 'title':
      $label = [
        '#theme' => 'form_element_label',
        '#title' => $element['#title'],
        '#required' => $show_component_required_marker,
        '#title_display' => 'before',
      ];
      $element['#title'] = render($label);
      break;
    case 'placeholder':
      $element['#attributes']['placeholder'] = $element['#title'];
      if ($show_component_required_marker) {
        $element['#attributes']['placeholder'] .= ' (' . t('Required') . ')';
      }
      $element['#title_display'] = 'invisible';
      break;
    case 'none':
      $element['#title_display'] = 'invisible';
      break;
    case 'attribute':
      $element['#title_display'] = 'attribute';
      $element['#attributes']['title'] = $element['#title'];
      if ($show_component_required_marker) {
        $element['#attributes']['title'] .= ' (' . t('Required') . ')';
      }
      break;
    case 'description':
    default:
      $label = [
        '#theme' => 'form_element_label',
        '#title' => $element['#title'],
        '#required' => $show_component_required_marker,
        '#title_display' => 'before',
      ];
      $element['#title_display'] = 'invisible';
      $element['#description'] = $label;
      $element['#after_build'][] = 'name_component_description_after_build_label_alter';
      break;
  }
  return $element;
}