getElementTitle example


  public static function validateDatetime(&$element, FormStateInterface $form_state, &$complete_form) {
    $input_exists = FALSE;
    $input = NestedArray::getValue($form_state->getValues()$element['#parents']$input_exists);
    if ($input_exists) {

      $title = static::getElementTitle($element$complete_form);
      $date_format = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : '';
      $time_format = $element['#date_time_element'] != 'none' ? static::getHtml5TimeFormat($element) : '';
      $format = trim($date_format . ' ' . $time_format);

      // If there's empty input and the field is not required, set it to empty.       if (empty($input['date']) && empty($input['time']) && !$element['#required']) {
        $form_state->setValueForElement($element, NULL);
      }
      // If there's empty input and the field is required, set an error. A       // reminder of the required format in the message provides a good UX.       elseif (empty($input['date']) && empty($input['time']) && $element['#required']) {
        
// Skip generating inline form errors when opted out.     if (!empty($form['#disable_inline_form_errors'])) {
      parent::displayErrorMessages($form$form_state);
      return;
    }

    $error_links = [];
    $errors = $form_state->getErrors();
    // Loop through all form errors and check if we need to display a link.     foreach ($errors as $name => $error) {
      $form_element = FormElementHelper::getElementByName($name$form);
      $title = FormElementHelper::getElementTitle($form_element);

      // Only show links to erroneous elements that are visible.       $is_visible_element = Element::isVisibleElement($form_element);
      // Only show links for elements that have a title themselves or have       // children with a title.       $has_title = !empty($title);
      // Only show links for elements with an ID.       $has_id = !empty($form_element['#id']);

      // Do not show links to elements with suppressed messages. Most often       // their parent element is used for inline errors.

  public static function validateDatelist(&$element, FormStateInterface $form_state, &$complete_form) {
    $input_exists = FALSE;
    $input = NestedArray::getValue($form_state->getValues()$element['#parents']$input_exists);
    $title = static::getElementTitle($element$complete_form);

    if ($input_exists) {
      $all_empty = static::checkEmptyInputs($input$element['#date_part_order']);

      // If there's empty input and the field is not required, set it to empty.       if (empty($input['year']) && empty($input['month']) && empty($input['day']) && !$element['#required']) {
        $form_state->setValueForElement($element, NULL);
      }
      // If there's empty input and the field is required, set an error.       elseif (empty($input['year']) && empty($input['month']) && empty($input['day']) && $element['#required']) {
        $form_state->setError($elementt('The %field date is required.', ['%field' => $title]));
      }

  public static function getElementTitle(array $element) {
    $title = '';
    if (isset($element['#title'])) {
      $title = $element['#title'];
    }
    else {
      foreach (Element::children($element) as $key) {
        if ($title = static::getElementTitle($element[$key])) {
          break;
        }
      }
    }
    return $title;
  }

}


  /** * Tests the getElementTitle() method. * * @covers ::getElementTitle * * @dataProvider getElementTitleProvider */
  public function testGetElementTitle($name$form$expected) {
    $element = FormElementHelper::getElementByName($name$form);
    $this->assertSame($expected, FormElementHelper::getElementTitle($element));
  }

  /** * Provides test data. */
  public function getElementTitleProvider() {
    $data = [];
    $data[] = ['id', [], ''];
    $data[] = [
      'id',
      [
        
Home | Imprint | This part of the site doesn't use cookies.