WebTechKitchen; Your Web Technology Kitchen, contact us to create, or maintain your websites and other digital properties.

Ajax calls in Drupal

Submitted by barnettech on Fri, 05/06/2022 - 10:29

To send the json data in JavaScript

   var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance
   xmlhttp.open("POST", "/user/17352/post-dashboard");
   xmlhttp.setRequestHeader("Content-Type", "application/json");

To receive the json data in PHP

 $json_string = \Drupal::request()->getContent();
    $decoded = \Drupal\Component\Serialization\Json::decode($json_string);
    $name = $decoded['name'];

    return new JsonResponse($name);

Platform.sh commands

Submitted by barnettech on Fri, 03/25/2022 - 10:12

Activate a new branch and have it register in platform.sh:

platform environment:activate new-feature

Importing data:

platform environment:list
| ID | Title | Status | Type |
| main | Main | Active | production |

platform sql < my_database_backup.sql
or if can't determine current environment:
platform sql -e main < talktoyourdata.sql

Exporting data:

platform db:dump --gzip

Here was an interesting prompt to ChatGPT: "For someone who very incorrectly translates Pirkei Avot 1:14: "אִם אֵין אֲנִי לִי, מִי לִי. וְכִשְׁאֲנ as meaning you should be very selfish first, what quote or passage from Pirkei Avot should they read next."

Submitted by barnettech on Fri, 03/18/2022 - 15:13

(I'm giving my webtechkitchen some good content to crawl for when I use the OPENAI_SEARCH module. Now I can promot Chat for some interesting ethical questions :)

Pirkei Avot 1:14:

"אִם אֵין אֲנִי לִי, מִי לִי. וְכִשְׁאֲנִי לְעַצְמִי, מָה אֲנִי. וְאִם לֹא עַכְשָׁוְ, אֵימָתָי."

Which translates to: "If I am not for myself, who will be for me? But if I am only for myself, what am I? And if not now, when?"

JavaScript post request

Submitted by barnettech on Wed, 03/02/2022 - 15:03
var email = document.querySelector('#edit-investor-profiles-0-entity-field-email-0-value').value;
  if (email.length > 3) {
    // construct an HTTP request
    var xhr = new XMLHttpRequest();
    var url = "/user/register/check-email"
    xhr.open("POST", url, true);
    xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

    var data = JSON.stringify({"email": email});
    // send the collected data as JSON
    xhr.onreadystatechange = function()
      if (xhr.readyState == 4 && xhr.status == 200)