8 – Why is my code called again written on build form?

I have a form and i call an API tahta brings huge data on build form.
But when i call Ajax on other form elements , the whole code for form(buildForm, constructor ) is called again and again. There is a way to check triggering element but the code is called twice again once due to ajax Callback and once due to validateForm. I want a solution so that the Api are only called once, what solution i can use for this

<?php

namespace Drupalfapi_exampleForm;

use DrupalCoreFormFormStateInterface;

/**
 * Implements the ajax demo form controller.
 *
 * This example demonstrates using ajax callbacks to populate the options of a
 * color select element dynamically based on the value selected in another
 * select element in the form.
 *
 * @see DrupalCoreFormFormBase
 * @see DrupalCoreFormConfigFormBase
 */
class AjaxDemo extends DemoBase {

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form('temperature') = (
      '#title' => $this->t('Temperature'),
      '#type' => 'select',
      '#options' => $this->getColorTemperatures(),
      '#empty_option' => $this->t('- Select a color temperature -'),
      '#ajax' => (
        // Could also use (get_class($this), 'updateColor').
        'callback' => '::updateColor',
        'wrapper' => 'color-wrapper',
      ),
    );
    print("API call");
    $form('color_wrapper') = (
      '#type' => 'container',
      '#attributes' => ('id' => 'color-wrapper'),
    );

    $temperature = $form_state->getValue('temperature');
    if (!empty($temperature)) {
      $form('color_wrapper')('color') = (
        '#type' => 'select',
        '#title' => $this->t('Color'),
        '#options' => $this->getColorsByTemperature($temperature),
      );
    }

    // Add a submit button that handles the submission of the form.
    $form('actions') = (
      '#type' => 'actions',
      'submit' => (
        '#type' => 'submit',
        '#value' => $this->t('Submit'),
      ),
    );

    return $form;
  }

  /**
   * Ajax callback for the color dropdown.
   */
  public function updateColor(array $form, FormStateInterface $form_state) {
    return $form('color_wrapper');
  }

  /**
   * Returns colors that correspond with the given temperature.
   *
   * @param string $temperature
   *   The color temperature for which to return a list of colors. Can be either
   *   'warm' or 'cool'.
   *
   * @return array
   *   An associative array of colors that correspond to the given color
   *   temperature, suitable to use as form options.
   */
  protected function getColorsByTemperature($temperature) {
    return $this->getColors()($temperature)('colors');
  }

  /**
   * Returns a list of color temperatures.
   *
   * @return array
   *   An associative array of color temperatures, suitable to use as form
   *   options.
   */
  protected function getColorTemperatures() {
    return array_map(function ($color_data) {
      return $color_data('name');
    }, $this->getColors());
  }

  public function validateForm(array &$form, FormStateInterface $form_state) {
    $form_values = $form_state->getValues();
    print("Sssaaa");
  }

  /**
   * Returns an array of colors grouped by color temperature.
   *
   * @return array
   *   An associative array of color data, keyed by color temperature.
   */
  protected function getColors() {
    return (
      'warm' => (
        'name' => $this->t('Warm'),
        'colors' => (
          'red' => $this->t('Red'),
          'orange' => $this->t('Orange'),
          'yellow' => $this->t('Yellow'),
        ),
      ),
      'cool' => (
        'name' => $this->t('Cool'),
        'colors' => (
          'blue' => $this->t('Blue'),
          'purple' => $this->t('Purple'),
          'green' => $this->t('Green'),
        ),
      ),
    );
  }

}

Drupal 8 Ajax form, Code called again written on build form

I have a form and i call an API tahta brings huge data on build form.
But when i call Ajax on other form elements , the whole code for form(buildForm, constructor ) is called again and again. There is a way to check triggering element but the code is called twice again once due to ajax Callback and once due to validateForm. I want a solution so that the Api are only called once, what solution i can use for this

<?php

namespace Drupalfapi_exampleForm;

use DrupalCoreFormFormStateInterface;

/**
 * Implements the ajax demo form controller.
 *
 * This example demonstrates using ajax callbacks to populate the options of a
 * color select element dynamically based on the value selected in another
 * select element in the form.
 *
 * @see DrupalCoreFormFormBase
 * @see DrupalCoreFormConfigFormBase
 */
class AjaxDemo extends DemoBase {

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form('temperature') = (
      '#title' => $this->t('Temperature'),
      '#type' => 'select',
      '#options' => $this->getColorTemperatures(),
      '#empty_option' => $this->t('- Select a color temperature -'),
      '#ajax' => (
        // Could also use (get_class($this), 'updateColor').
        'callback' => '::updateColor',
        'wrapper' => 'color-wrapper',
      ),
    );
    print("API call");
    $form('color_wrapper') = (
      '#type' => 'container',
      '#attributes' => ('id' => 'color-wrapper'),
    );

    $temperature = $form_state->getValue('temperature');
    if (!empty($temperature)) {
      $form('color_wrapper')('color') = (
        '#type' => 'select',
        '#title' => $this->t('Color'),
        '#options' => $this->getColorsByTemperature($temperature),
      );
    }

    // Add a submit button that handles the submission of the form.
    $form('actions') = (
      '#type' => 'actions',
      'submit' => (
        '#type' => 'submit',
        '#value' => $this->t('Submit'),
      ),
    );

    return $form;
  }

  /**
   * Ajax callback for the color dropdown.
   */
  public function updateColor(array $form, FormStateInterface $form_state) {
    return $form('color_wrapper');
  }

  /**
   * Returns colors that correspond with the given temperature.
   *
   * @param string $temperature
   *   The color temperature for which to return a list of colors. Can be either
   *   'warm' or 'cool'.
   *
   * @return array
   *   An associative array of colors that correspond to the given color
   *   temperature, suitable to use as form options.
   */
  protected function getColorsByTemperature($temperature) {
    return $this->getColors()($temperature)('colors');
  }

  /**
   * Returns a list of color temperatures.
   *
   * @return array
   *   An associative array of color temperatures, suitable to use as form
   *   options.
   */
  protected function getColorTemperatures() {
    return array_map(function ($color_data) {
      return $color_data('name');
    }, $this->getColors());
  }

  public function validateForm(array &$form, FormStateInterface $form_state) {
    $form_values = $form_state->getValues();
    print("Sssaaa");
  }

  /**
   * Returns an array of colors grouped by color temperature.
   *
   * @return array
   *   An associative array of color data, keyed by color temperature.
   */
  protected function getColors() {
    return (
      'warm' => (
        'name' => $this->t('Warm'),
        'colors' => (
          'red' => $this->t('Red'),
          'orange' => $this->t('Orange'),
          'yellow' => $this->t('Yellow'),
        ),
      ),
      'cool' => (
        'name' => $this->t('Cool'),
        'colors' => (
          'blue' => $this->t('Blue'),
          'purple' => $this->t('Purple'),
          'green' => $this->t('Green'),
        ),
      ),
    );
  }

}

visas – What countries in the Schengen area accept US travel document (form I-571)

I’m planning a trip to Europe this summer (Italy, Spain and Portugal) and will be using my travel document (Form I-571) that I have based on asylum from Syria. I had a visa appointment at the Spanish consulate today and was not able to submit the application as the officer mentioned that Spain does not recognize refugee/asylee travel documents.

This wikipedia page mentions that Spain accepts U.S. travel documents, however, I’ve read other sources that site the opposite such as this thread. Do you know which countries accept a travel document (with a Schengen visa) in 2019? I’d appreciate if you can include an official website, I’m having a hard time finding any.

Remove Catalina update files form High Sierra

I downloaded the Catalina (or perhaps Big Sur) update on my High Sierra system, during the install, it told me a couple of apps won’t run on the new OS (I have some 32 bit apps that won’t be updated to 64 bit until the summer). I need those apps to work for now, so I quit the install. But now every day I get a reminder to update, or be reminded again tomorrow. No way to opt out is given.

How can I remove the update files to avoid the pesky daily reminder?

How to deal with browser navigation in multi step form with validation

I am not a designer nor UX person. I am implementing an application which has multi-step form. Imagine that in this form some steps (screens) need to be validated before person can proceed to next step.

The application has also its own prev/next buttons with progress (dots), showing the person remaining amount of steps needed to be taken.

I am having issue with how to deal with browser navigation. Ideally I want each step of the form to have its own URL. This means URL would control what is rendered (some data for the form is cached in the browser and prefilled for the user). But what happens when user uses browser navigation to visit an URL that should not be allowed?

Imagine following scenario:

I have screen 1 containing a form and screen 2 containing some content. User needs to fill the form in screen 1 to proceed to screen 2. Let’s say user fills the form and is redirected to screen 2. Now the user decides to go back, either by using application navigation or browser navigation (both is possible). The scenario which I’m trying to figure out is when user clears the form and uses browser back button. At this point the application is in invalid state.

How to deal with this? I don’t know what is the right approach. Maybe I should not let user go to URL that should not be visible to him and instead redirect him to screen 1? But that could be problematic as user would not get any indication.

magento2.3.4 – Magento2 – How to fetch the admin product form data in ajax

We are trying to show some information before saving the product via an ajax call.

In this ajax, we need to pass all the product form data.

The initial thought was to serialize the product form. But cannot see the form tag in the product add/edit area.

How can we get this product form data in javascript to pass via ajax?

Integrate blog post image into user e-mail within contact form 7 plugin

I am using contact form 7 on blog posts for lead generation for properties. I want to integrate the blog post image in the contact form 7 HTML-e-Mail response so the user sees the image. Is there any possibility to use contact form 7 features to integrate it directly? How do I get the correct image URL within the contact form 7 e-Mail HTML-code?

Thanks for your support!

Best,
Nico

8 – How to send a form back a step in a multistep Webform

I am trying to figure out how to programmatically boot a user back to an earlier step if they fail validation or submission on one of the final steps of a form. The form has to check some remote service calls before accepting the submission.

I tried to add a validate hook and try something simple:

function mymodule_schedule_validate($form, FormStateInterface $form_state) {
  $page = $form_state->get('current_page');

  if ($page == 'step_2') {
    $form_state->setErrorByName('tour_date', 'Pick some other thing or whatever');
    $form_state->set('current_page', 'step_1');
    $form_state->setRebuild();
    #$form_state->setRedirect(new RedirectResponse(DrupalCoreUrl::fromUserInput('/form/schedule')->getRouteName()));
  }
}

While the code is hit when step 2 is submitted, and it sets current page to step_1, nothing else happens. The error appears, but the form displayed in the modal is still step 2. I tried with and without the redirect.

How can I manipulate the form this way?

Highlight missing form fields one at a time or all at once?

A form has many required fields some of which have been left blank. When the user clicks the submit button, there is functionality in place to highlight the first required-but-empty field by giving it a red outline and a warning icon beside it. Is this ideal (as it doesn’t overwhelm user with warning colors and warning icons all over the form) or would it be better UX to highlight all missing fields at once?

Google Sheet script will not run on a tab linked to a Google Form

I wrote a script to move a row of data from one sheet (Sheet1) to another (Sheet2) when I type the word Yes in column one. I can get the script to work perfectly when the sheet is not collecting Form responses. When I have the first tab attached to a form my script will not run. Can someone please tell me what would prevent the script from running on a tab that is collecting responses?