0
<?php
namespace Drupal\dishes_dropdown\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Component\Utility\UrlHelper;

/**
 * Desarrollo de un formulario en drupal 8
 *
 */
class DishesDropdownForm extends FormBase {


    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'dishes_dropdown_form';
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {

        $maxlength = 250;

        $tax_terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('dish_type', $parent=0, $max_depth=NULL, $load_entities=FALSE);
        $taxs = array();
        foreach ($tax_terms as $tax_term) {
            $taxs[$tax_term->tid] = $tax_term->name;
        }

        $form['dish_dropdown'] = array(
            '#type' => 'select',
            '#options' => $taxs,
            '#chosen' => true,
            '#multiple' => false,
            '#maxlength' => $maxlength,
            '#default_value' => $form_state->hasValue('dish_dropdown') ? $form_state->getValue('dish_dropdown') : '' ,

        $form['submit'] = [
            '#type'  => 'submit',
            '#value' => $this->t('Ir al tipo de plato'),
            '#weight' => 50,
        ];
        return $form;
    }

    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        // Display result.
        $id = reset($form_state-> getValues('dish_dropdown'));
        $aliasManager = \Drupal::service('path.alias_manager');
        // The second argument to getAliasByPath is a language code such as "en" or LanguageInterface::DEFAULT_LANGUAGE.
        $alias = $aliasManager->getAliasByPath('/taxonomy/term/' . $id);

        $url = new TrustedRedirectResponse($alias);
        if (isset($url)) {
            $form_state-> setResponse($url);
        }

        return;


    }
}

I do not load the "default_value" in select. I have tried a thousand ways in the "build" but there is no way. I want a blank default value but without forcing it load it. It's like if you read the "default_value".

1 Answer 1

1

The #default_value for a select element should be an integer value matching an array key in the #options array.

In your case, lets say

$taxs = [5 => 'blue dish', 9 => 'red dish', 15 => 'green dish'];

Then an option for the value for *#default_value will be one of 5,9 or 15.

In your case the function $form_state->hasValue('dish_dropdown') will return one of "blue dish", "red dish" or "green dish" which is not an array key in your $taxs array.

So what do you do?

  1. Either select one of your dish types as the default OR
  2. Add a default value - None -

For example:

$taxs[0] = '-- None --';
$reverseTaxs = array_flip($taxs);
$form['dish_dropdown'] = array(
        '#type' => 'select',
        '#options' => $taxs,
        '#chosen' => true,
        '#multiple' => false,
        '#maxlength' => $maxlength,
        '#default_value' => $form_state->hasValue('dish_dropdown') ? $reverseTaxs[$form_state->getValue('dish_dropdown')] : 0 ,

    $form['submit'] = [
        '#type'  => 'submit',
        '#value' => $this->t('Ir al tipo de plato'),
        '#weight' => 50,
    ];
    return $form;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.