webforms – How can I enforce a default machine name prefix?

By adding this code to a module, administrators can create forms that will can be exported to config while other users with permissions to create webforms will automatically have the machine name set with the site_ prefix.

function webform_create_validation(&$form, FormStateInterface $form_state) {
  $current_user = Drupal::currentUser();
  // If user creating the webform is not an administrator.
  if (!in_array('administrator', $current_user->getRoles())) {
    // Get machine name of webform being created.
    $ent = $form_state->getFormObject()->getEntity();
    if (isset($ent)) {
      $id = $ent->id();
      // Check to see if webform machine name starts with "site_".
      // If it does not, add it and save.
      if (substr($id, 0, 5) !== "site_") {
        $new_id = 'site_' . $id;
        $form_state->setValue('id', $new_id);
      }
    }
  }
}

How to send all theme_mod customizer default values to the database upon theme installation?

I’m building a new theme with many customizer settings, which all have default values.

I need these default values to be sent to the database immediately upon theme installation, so that they appear on the front end of the website.

Is there a simple way to do this?

magento2 – How do we change the Default Stock or Default Source for Magento 2 MSI?

This is because MSI isn’t complete yet – there is functionality not yet migrated to it.

The default stock is hard coded because of that – backwards compatibility.

There are many extensions and even parts of Magento that still rely on “inventory 1.0” and so the default is hard coded so they don’t break.
My default stock mysteriously acquires amounts at times, positive and negative.

This in between status is painful but hey, it’s only for another year or so 😉

For example for bundles to work, products must be “in stock” in default stock (real stock or “in stock” status with “back order” on, because bundles aren’t MSI migrated yet. It’s really annoying, but hey, bundles are hard. (the good news are, when you ship it ships properly from your stock)

When MSI is at 100% coverage, we will be able to change who default is.

How do I configure Gmail to collapse inbox sections by default when using Multiple Inboxes?

I use Gmails Multiple Inboxes setting in which I have set up 3 Inbox sections (which are all work related) as follows:
multiple inbox sections settings

All inbox sections have some e-mails in them which are all visible when I open my Gmail Inbox.

Since they’re all work related inboxes I would like to be able to collapse these Inboxes in my evenings and weekends and have them stay collapsed after a page refresh but unfortunately they’re open again after a page refresh.

Is there a way to configure that the Inbox sections should be collapsed by default or that they remember the last open / closed state?

NGINX setup for REST API returning default configuration

I’ve hired a VPS to play with some personal projects. To get started, I’m trying to set it up to host a REST API using Node.js, as I’ve only ever used Spring Boot for that.

I’ve implemented the solutions by adapting from the following guides:

https://www.robinwieruch.de/node-express-server-rest-api (most of the API code looks like this)

https://itnext.io/building-restful-api-with-node-js-express-js-and-postgresql-the-right-way-b2e718ad1c66 (but I’m slowly converting to use the standards from here)

For the actual deployment, I’ve changed to bundle the API with Babel and I’m deploying it with PM2.

https://www.nginx.com/blog/deploying-nginx-plus-as-an-api-gateway-part-1/ (not Plus though)

(All those links are available via http://web.archive.org/ so they aren’t going anywhere anytime soon)

I created another Node.js project using axios to test the REST requests. Running it in the same VPS works, but I had to change the API code to bind to localhost. Before, with the binding address unspecified, it was binding to an IPv6 localhost (if I understood correctly) and, since my domain doesn’t work with IPv6, I’ll stick with IPv4.

On the NGINX side I’ve made the most changes as I only have 1 API now and I won’t be using load balancing. Also, I’ve changed the naming policy. I’ll be using example.com/app_or_project_name/api_or_web_or_other_kind_of_interface/project_specific_routes.

Here’s how my NGINX setup is looking like. I’ve anonymized it and changed to use the same name as the NGINX example:

api_backends.conf

upstream warehouse {
    zone api 64k;
    server 127.0.0.1:some_port_number;
}

That’s the REST API host and port number.

api_conf.d/warehouse_api.conf

# Warehouse API
#
location /warehouse/api/ {
    # Policy configuration here (authentication, rate limiting, logging, more...)
    #
    access_log /var/log/nginx/warehouse_api.log main;
    auth_request /_validate_apikey;

    # URI routing
    #
    proxy_pass http://warehouse;

    return 404; # Catch-all
}

api_gateway.conf

include api_backends.conf;
include api_keys.conf;

server {
    access_log /var/log/nginx/api_access.log main; # Each API may also log to a separate file

    listen 443 ssl;
    server_name my-domain.net;

    # TLS config
    ssl_certificate /etc/letsencrypt/live/my-domain.net/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my-domain.net/privkey.pem; # managed by Certbot
    ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout  5m;
    ssl_ciphers          HIGH:!aNULL:!MD5;
    ssl_protocols        TLSv1.2 TLSv1.3;

    # API definitions, one per file
    include api_conf.d/*.conf;

    # Error responses
    # error_page 404 = @400;         # Invalid paths are treated as bad requests
    proxy_intercept_errors on;     # Do not send backend errors to the client
    include api_json_errors.conf;  # API client friendly JSON error responses
    default_type application/json; # If no content-type then assume JSON

    # API key validation
    location = /_validate_apikey {
        internal;

        if ($http_apikey = "") {
            return 401; # Unauthorized
        }
        if ($api_client_name = "") {
            return 403; # Forbidden
        }

        return 204; # OK (no content)
    }

}

I might set proxy_intercept_errors to off once it’s working. I’ll have to make some tests to see what changes in the responses.

api_json_errors.conf

Same as example.

default.conf

server {
    server_name  www.my-domain.net;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ .php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ .php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #    deny  all;
    #}

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/my-domain.net/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my-domain.net/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = www.my-domain.net) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = my-domain.net) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen       80;
    server_name  my-domain.net www.my-domain.net;
    return 404; # managed by Certbot

}

I had to make some changes here because there was a combination of address and port that had a duplicate server configuration.

nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log info;
pid        /var/run/nginx.pid;

load_module /etc/nginx/modules/ngx_http_js_module.so;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user ($time_local) "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    include /etc/nginx/api_gateway.conf; # All API gateway configuration
    include /etc/nginx/conf.d/*.conf;    # Regular web traffic
}

When I run the same test project outside the VPS, I get the following result:

{
  message: 'Request failed with status code 404',
  name: 'Error',
  description: undefined,
  number: undefined,
  fileName: undefined,
  lineNumber: undefined,
  columnNumber: undefined,
  stack: '...',
  config: {
    url: 'https://my-domain.net/warehouse/api/messages',
    method: 'get',
    headers: {
      Accept: 'application/json, text/plain, */*',
      'Access-Control-Allow-Origin': '*',
      'User-Agent': 'axios/0.21.1'
    },
    transformRequest: ( (Function: transformRequest) ),
    transformResponse: ( (Function: transformResponse) ),
    timeout: 0,
    adapter: (Function: httpAdapter),
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    validateStatus: (Function: validateStatus),
    apikey: '...',
    data: undefined
  },
  code: undefined
}

One thing I managed to figure out is that the 404 error is returned by warehouse_api.conf because, if I change return 404; to another code, that’s the code I’ll get.

I’ve enabled debugging in NGINX but I couldn’t understand the output, even after searching a little:

2021/07/22 11:54:17 (debug) nginx_pid#nginx_pid: *757 using location: @404 "/warehouse/api/messages?"
2021/07/22 11:54:31 (debug) nginx_pid#nginx_pid: *758 http cl:-1 max:1048576
2021/07/22 11:54:31 (debug) nginx_pid#nginx_pid: *758 rewrite phase: 3
2021/07/22 11:54:31 (debug) nginx_pid#nginx_pid: *758 http finalize request: 404, "/warehouse/api/messages?" a:1, c:1
2021/07/22 11:54:31 (debug) nginx_pid#nginx_pid: *758 http special response: 404, "/warehouse/api/messages?"
2021/07/22 11:54:31 (debug) nginx_pid#nginx_pid: *758 test location: "@400"
2021/07/22 11:54:31 (debug) nginx_pid#nginx_pid: *758 test location: "@401"
2021/07/22 11:54:31 (debug) nginx_pid#nginx_pid: *758 test location: "@403"
2021/07/22 11:54:31 (debug) nginx_pid#nginx_pid: *758 test location: "@404"
2021/07/22 11:54:31 (debug) nginx_pid#nginx_pid: *758 using location: @404 "/warehouse/api/messages?"

I tried a few different approaches to search about all this but couldn’t find any leads.

So, what’s going on, what’s wrong and how do I fix it?

Thanks in advance.

magento2 – Add default date from filter to UI Component grid in Magento 2

I have a custom grid created in the backend for a Magento 2.4.1 instance. Everything is working properly (filters, sorting, etc.), but I have one more request that seems to be more complicated.

I have multiple date range filters (from, to) and for one of them I would like to have a default filter that should be loaded when the page loads. For example, the date add column has a date range filter and I would like to filter the results by default in order to load only those records created in the last 60 days. (date_add from >= 60 days before today).

The grid was created using UI Components and I was able to add the following code under <filters …>.

<filters name="listing_filters">
      <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
          <item name="displayArea" xsi:type="string">dataGridFilters</item>
          <item name="dataScope" xsi:type="string">filters</item>
          <item name="applied" xsi:type="array">
              <item name="date_add" xsi:type="string">WHAT TO PUT HERE?</item>
          </item>
...

How can I do this using my current code?
Do you have maybe other solutions?

Set default value from HTML selection in PHP funtion

I am working on my first plugin where a user can select an option from a dropdown and this selection gets saved in a code that gets written into the footer. That works, but the problem is, my default selection (as in the HTML) gets ignored.

The selection

<select id="location_select" name="location_select">
 <option selected value="ch" <?php selected(get_option('location_select'), 'ch'); ?>><?php _e('CH', 'pluginName') ; ?></option>
 <option value="de" <?php selected(get_option('location_select'), 'de'); ?>><?php _e('DE', 'pluginName') ; ?></option>
</select>

The code

public function print_code(){
        $location   =   get_option('location_select');
        $url        =   'https://' . esc_html($location) . '.example.com/js/script.js';
        wp_enqueue_script('myID', $url, array('jquery'), null, true);

     }

     public function additional_attrs($tag, $handle, $src){
        if ($handle === 'myID'){
            $tag = '<script data-host="https://example.com" data-dnt="false" src="' . esc_url( $src ) . '" id="myID" async defer></script>';
        }
        return $tag;
     }

How is it possible to have the first selection <option selected … as a default selection in the code? Because right now, if a user does not save his selection, nothing gets written in the output code.

Thanks for any help!

plugin development – Add default value from selection

working on my first plugin where a user can select an option from a dropdown and this selection gets saved in a code that gets written into the footer. That works, but the problem is, my default selection (as in the HTML) gets ignored.

The selection

<select id="location_select" name="location_select">
 <option selected value="ch" <?php selected(get_option('location_select'), 'ch'); ?>><?php _e('CH', 'pluginName') ; ?></option>
 <option value="de" <?php selected(get_option('location_select'), 'de'); ?>><?php _e('DE', 'pluginName') ; ?></option>
</select>

The code

public function print_code(){
        $location   =   get_option('location_select');
        $url        =   'https://' . esc_html($location) . '.domain.com/js/script.js';
        wp_enqueue_script('myID', $url, array('jquery'), null, true);

     }

     public function additional_attrs($tag, $handle, $src){
        if ($handle === 'myID'){
            $tag = '<script data-host="https://domain.com" data-dnt="false" src="' . esc_url( $src ) . '" id="myID" async defer></script>';
        }
        return $tag;
     }

How is it possible to have the first selection <option selected ... as a default selection in the code? Because right now, if a user does not save his selection, nothing gets written in the output code.

Thanks for any help!

Change the default output form in mathematica 12

Is there a simple way I can change the default output form in Mathematica 12? In Mathematica 11, this option was simply in the "preference", but I can’t find it anywhere in Mathematica 12.

privacy – Does NW.js send “crash reports” to Google (or anyone external) by default?

https://nwjs.readthedocs.io/en/latest/References/Manifest%20Format/

crash_report_url

Once the app crashed, the crash dump file and information about the runtime environment will be sent to the crash server. It’s sent in the same way as in Chromium browser: a HTTP POST request with multipart/form-data as the content type. In theory, any breakpad/crashpad server could handle the request, since breakpad/crashpad work in the same way in NW as they do in Chromium.

Nothing is said about any default value/behaviour. I get the gut feeling that, by default, if a NW.js application crashes, it sends a bunch of data (“crash report”) to some Chromium (= Google) server.

Can somebody please verify that this is/isn’t the case? And what should I set this value to in order for it to never happen?

I don’t want any data to leave my NW.js application for any reason unless I very explicitly tell it to.

PS: I would ask on their Github repo if Github allowed me to register/ask. Which it doesn’t.