AJAX Modal Form – Drupal Answers

Created a modal form in AJAX


use DrupalCoreAjaxAjaxResponse;
use DrupalCoreAjax;
use DrupalCoreAjaxOpenModalDialogCommand;


public function open_modal(&$form, FormStateInterface $form_state) {
    $node_title = $form_state->getValue('node_title');
    $query = Drupal::entityQuery('node')->condition('title', $node_title, 'CONTAINS');
    $entity = $query->execute();
    $key = array_keys($entity);
    $id = !empty($key(0)) ? $key(0) : NULL;
    $response = new AjaxResponse();
    $title = 'Node ID';
    if ($id !== NULL) {
      $content = '<div class="test-popup-content"> Node ID is: ' . $id . '</div>';
      $options = array(
        'dialogClass' => 'popup-dialog-class',
        'width' => '300',
        'height' => '300',    
      );
      $response->addCommand(new OpenModalDialogCommand($title, $content, $options));
    } else {
      $content = 'Not found record with this title <strong>' . $node_title . '</strong>';
      $options = array(
        'dialogClass' => 'popup-dialog-class',
        'width' => '300',
        'height' => '300',
      );
      $response->addCommand(new OpenModalDialogCommand($title, $content, $options));
    }
    return $response; 
  }

Error Message on calling Modal form

User error: “type” is an invalid render array key in DrupalCoreRenderElement::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).
DrupalCoreRenderElement::children(Array) (Line: 1027)
DrupalCoreFormFormBuilder->doBuildForm(‘hello_model_form’, Array, Object) (Line: 1073)
DrupalCoreFormFormBuilder->doBuildForm(‘hello_model_form’, Array, Object) (Line: 575)
DrupalCoreFormFormBuilder->processForm(‘hello_model_form’, Array, Object) (Line: 321)
DrupalCoreFormFormBuilder->buildForm(Object, Object) (Line: 91)
DrupalCoreControllerFormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->DrupalCoreEventSubscriber{closure}() (Line: 573)
DrupalCoreRenderRenderer->executeInRenderContext(Object, Object) (Line: 124)
DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->DrupalCoreEventSubscriber{closure}() (Line: 151)
SymfonyComponentHttpKernelHttpKernel->handleRaw(Object, 1) (Line: 68)
SymfonyComponentHttpKernelHttpKernel->handle(Object, 1, 1) (Line: 57)
DrupalCoreStackMiddlewareSession->handle(Object, 1, 1) (Line: 47)
DrupalCoreStackMiddlewareKernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupalpage_cacheStackMiddlewarePageCache->pass(Object, 1, 1) (Line: 85)
Drupalpage_cacheStackMiddlewarePageCache->handle(Object, 1, 1) (Line: 47)
DrupalCoreStackMiddlewareReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
DrupalCoreStackMiddlewareNegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
StackStackedHttpKernel->handle(Object, 1, 1) (Line: 708)
DrupalCoreDrupalKernel->handle(Object) (Line: 19)
User error: “type” is an invalid render array key in DrupalCoreRenderElement::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).
DrupalCoreRenderElement::children(Array, ) (Line: 402)
DrupalCoreRenderRenderer->doRender(Array) (Line: 444)
DrupalCoreRenderRenderer->doRender(Array, ) (Line: 200)
DrupalCoreRenderRenderer->render(Array, ) (Line: 226)
DrupalCoreRenderMainContentHtmlRenderer->DrupalCoreRenderMainContent{closure}() (Line: 573)
DrupalCoreRenderRenderer->executeInRenderContext(Object, Object) (Line: 227)
DrupalCoreRenderMainContentHtmlRenderer->prepare(Array, Object, Object) (Line: 117)
DrupalCoreRenderMainContentHtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
DrupalCoreEventSubscriberMainContentViewSubscriber->onViewRenderArray(Object, ‘kernel.view’, Object)
call_user_func(Array, Object, ‘kernel.view’, Object) (Line: 111)
DrupalComponentEventDispatcherContainerAwareEventDispatcher->dispatch(‘kernel.view’, Object) (Line: 156)
SymfonyComponentHttpKernelHttpKernel->handleRaw(Object, 1) (Line: 68)
SymfonyComponentHttpKernelHttpKernel->handle(Object, 1, 1) (Line: 57)
DrupalCoreStackMiddlewareSession->handle(Object, 1, 1) (Line: 47)
DrupalCoreStackMiddlewareKernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupalpage_cacheStackMiddlewarePageCache->pass(Object, 1, 1) (Line: 85)
Drupalpage_cacheStackMiddlewarePageCache->handle(Object, 1, 1) (Line: 47)
DrupalCoreStackMiddlewareReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
DrupalCoreStackMiddlewareNegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
StackStackedHttpKernel->handle(Object, 1, 1) (Line: 708)
DrupalCoreDrupalKernel->handle(Object) (Line: 19)

nginx – I am trying to set up a drupal site using docker in a Vagrant VM and can’t seem to get Certbot to work

So I am trying to set up a drupal site using docker in a vagrant VM using bento/ubuntu-20.04.

I went through this tutorial: https://www.digitalocean.com/community/tutorials/how-to-install-drupal-with-docker-compose . It seemed to work (I could navigate to the url I was using http 80 before I tried to set up https) but trying to implement the certbot setup it didn’t work. I don’t think it is the certbot directly because when I do: docker-compose exec webserver ls -lsh /etc/letsencrypt/live/server.otherrealm.org there seems to be a valid certificate. However, when I try to view the site using https/443, it does not work (gives ‘The connection has timed out’). I can’t see anything wrong with my configuration but I’ve been staring at it for the past day and I need someone else to take a fresh look at it. THANKS!! Let me know if there is anything that is not clear.

nginx-conf/nginx.conf

server {
    listen 80;
    listen (::):80;
    server_name server.otherrealm.org;

    location ~ /.well-known/acme-challenge {
        allow all;
        root /var/www/html;
    }

    location / {
        rewrite ^ https://$host$request_uri? permanent;
    }
}
server {
    listen 443 ssl;
    listen (::):443 ssl;
    server_name server.otherrealm.org;

    index index.php index.html index.htm;

    root /var/www/html;

    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/server.otherrealm.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/server.otherrealm.org/privkey.pem;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    rewrite ^/core/authorize.php/core/authorize.php(.*)$ /core/authorize.php$1;

    location ~ .php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass drupal:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /.ht {
        deny all;
    }

    location = /favicon.ico {
        log_not_found off; access_log off;
    }
    location = /robots.txt {
        log_not_found off; access_log off; allow all;
    }
    location ~* .(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
}

docker-compose.yml

version: '3.8'
services:
  mysql:
    image: mysql:latest
    container_name: mysql
    restart: unless-stopped
    env_file: .env
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - internal
  drupal:
    image: drupal:fpm-alpine
    container_name: drupal
    depends_on:
      - mysql
    restart: unless-stopped
    networks:
      - internal
      - external
    volumes:
      - drupal-data:/var/www/html
  webserver:
    image: nginx:latest
    container_name: webserver
    depends_on:
      - drupal
    restart: unless-stopped
    ports:
      - 80:80
      - 443:443
    volumes:
      - drupal-data:/var/www/html
      - ./nginx-conf:/etc/nginx/conf.d
      - certbot-etc:/etc/letsencrypt
    networks:
      - external
  certbot:
    depends_on:
      - webserver
    image: certbot/dns-google
    container_name: certbot
    volumes:
      - certbot-etc:/etc/letsencrypt
      - drupal-data:/var/www/html
    command: certonly -d (sub.example.com) --webroot --webroot-path=/var/www/html --email (someone@example.com) --agree-tos --no-eff-email --force-renewal 

networks:
  external:
    driver: bridge
  internal:
    driver: bridge

volumes:
  drupal-data:
  db-data:
  certbot-etc:

vagrant@manager:/vagrant$ docker-compose ps

 Name                 Command               State                     Ports
----------------------------------------------------------------------------------------------
certbot     certbot certonly --webroot ...   Exit 0
drupal      docker-php-entrypoint php-fpm    Up       9000/tcp
mysql       docker-entrypoint.sh mysqld      Up       3306/tcp, 33060/tcp
webserver   /docker-entrypoint.sh ngin ...   Up       0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

vagrant@manager:/vagrant$ docker-compose logs certbot
(Of course, now it is saying ...too many certificates already issued for exact set of domains... but that is because I tried it too many times 🤦‍♂️😒)

Attaching to certbot
certbot      | Saving debug log to /var/log/letsencrypt/letsencrypt.log
certbot      | Plugins selected: Authenticator webroot, Installer None
certbot      | Renewing an existing certificate
certbot      | IMPORTANT NOTES:
certbot      |  - Congratulations! Your certificate and chain have been saved at:
certbot      |    /etc/letsencrypt/live/(server.example.com)/fullchain.pem
certbot      |    Your key file has been saved at:
certbot      |    /etc/letsencrypt/live/(server.example.com)/privkey.pem
certbot      |    Your cert will expire on 2021-01-24. To obtain a new or tweaked
certbot      |    version of this certificate in the future, simply run certbot
certbot      |    again. To non-interactively renew *all* of your certificates, run
certbot      |    "certbot renew"
certbot      |  - If you like Certbot, please consider supporting our work by:
certbot      |
certbot      |    Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
certbot      |    Donating to EFF:                    https://eff.org/donate-le
certbot      |
certbot      | Saving debug log to /var/log/letsencrypt/letsencrypt.log
certbot      | Plugins selected: Authenticator webroot, Installer None
certbot      | Renewing an existing certificate
certbot      | IMPORTANT NOTES:
certbot      |  - Congratulations! Your certificate and chain have been saved at:
certbot      |    /etc/letsencrypt/live/(server.example.com)/fullchain.pem
certbot      |    Your key file has been saved at:
certbot      |    /etc/letsencrypt/live/(server.example.com)/privkey.pem
certbot      |    Your cert will expire on 2021-01-24. To obtain a new or tweaked
certbot      |    version of this certificate in the future, simply run certbot
certbot      |    again. To non-interactively renew *all* of your certificates, run
certbot      |    "certbot renew"
certbot      |  - If you like Certbot, please consider supporting our work by:
certbot      |
certbot      |    Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
certbot      |    Donating to EFF:                    https://eff.org/donate-le
certbot      |

vagrant@manager:/vagrant$ docker-compose logs webserver

Attaching to webserver
webserver    | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform 
configuration
webserver    | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
webserver    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
webserver    | 10-listen-on-ipv6-by-default.sh: error: /etc/nginx/conf.d/default.conf is not a file or does not exist
webserver    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh  
webserver    | /docker-entrypoint.sh: Configuration complete; ready for start up
webserver    | 10.0.2.2 - - (26/Oct/2020:23:16:28 +0000) "GET / HTTP/1.0" 301 169 "-" "masscan/1.0 (https://github.com/robertdavidgraham/masscan)" "-"

Drupal JSON:API authorization – Drupal Answers

I’m at the early stages of trying to figure out the Drupal JSON API and can’t figure out what’s causing Some resources have been omitted because of insufficient authorization and The current user is not allowed to GET the selected resource in the response.

Is there a header I should be using? It’s all local so there’s no basic auth to get past.

My code:

  fetch('http://localhost/domain-6/jsonapi/group_content/microsite-group_menu-main', {
    method: 'GET',
    mode: 'no-cors',
    headers: {
      'Accept': 'application/vnd.api+json',
      'Content-Type': 'application/vnd.api+json'
    }
  })
  .then(response => response.json())
  .then(data => {
    console.log(data);
  })
  .catch((error) => {
    console.error('Error: ', error);
  });

The response was flagged as spam so I can’t post it here but it’s at https://pastebin.com/9M4kYyR6

how to run module or plugin functionality on everypage in drupal 8

I have drupal 8 website and I am trying to create google login onetap module but I am stuck to run on everypage and node or article.

Upgrade from drupal 8 to drupal 9 via composer

I am currently trying to upgrade my site from drupal 8.9 to drupal 9. I ran these commands to give permissions:

$ chmod 777 web/sites/default
$ find web/sites/default -name "*settings.php" -exec chmod 777 {} ;
$ find web/sites/default -name "*services.yml" -exec chmod 777 {} ;

Then I required the the recommended project for drupal 9 and also the dev:

$ composer require drupal/core-recommended:^9.0.0 drupal/core-composer-scaffold:^9.0.0 drupal/core-project-message:^9.0.0 --update-with-dependencies --no-update
$ composer require drupal/core-dev:^9.0.0 --dev --update-with-dependencies --no-update

All my modules are up to date and I also ran upgrade status to check for errors. I got one error, which was in tests (I guess that would be OK?) and a couple of warnings for a library that wasn’t installed.

Then I ran

$ composer update

and got the following errors:

Problem 1
- drupal/core-recommended 9.0.7 requires drupal/core 9.0.7 -> satisfiable by drupal/core(9.0.7).
- Conclusion: don't install drupal/core 9.0.7
- drupal/core-recommended 9.0.6 requires drupal/core 9.0.6 -> satisfiable by drupal/core(9.0.6).
- Conclusion: don't install drupal/core 9.0.6
- drupal/core-recommended 9.0.5 requires drupal/core 9.0.5 -> satisfiable by drupal/core(9.0.5).
- Conclusion: don't install drupal/core 9.0.5
- drupal/core-recommended 9.0.4 requires drupal/core 9.0.4 -> satisfiable by drupal/core(9.0.4).
- Conclusion: don't install drupal/core 9.0.4
- drupal/core-recommended 9.0.3 requires drupal/core 9.0.3 -> satisfiable by drupal/core(9.0.3).
- Conclusion: don't install drupal/core 9.0.3
- drupal/core-recommended 9.0.2 requires drupal/core 9.0.2 -> satisfiable by drupal/core(9.0.2).
- Conclusion: don't install drupal/core 9.0.2
- drupal/core-recommended 9.0.1 requires drupal/core 9.0.1 -> satisfiable by drupal/core(9.0.1).
- Conclusion: don't install drupal/core 9.0.1
- drupal/core-recommended 9.0.0 requires drupal/core 9.0.0 -> satisfiable by drupal/core(9.0.0).
- Conclusion: don't install drupal/core 9.0.0
- drupal/core-recommended 9.0.0-rc1 requires drupal/core 9.0.0-rc1 -> satisfiable by drupal/core(9.0.0-rc1).
- Conclusion: don't install drupal/core 9.0.0-rc1
- drupal/core-recommended 9.0.0-beta3 requires drupal/core 9.0.0-beta3 -> satisfiable by drupal/core(9.0.0-beta3).
- Conclusion: don't install drupal/core 9.0.0-beta3
- drupal/core-recommended 9.1.0-alpha1 requires drupal/core 9.1.0-alpha1 -> satisfiable by drupal/core(9.1.0-alpha1).
- Conclusion: don't install drupal/core 9.1.0-alpha1
- drupal/core-recommended 9.2.x-dev requires drupal/core 9.2.x-dev -> satisfiable by drupal/core(9.2.x-dev).
- Conclusion: don't install drupal/core 9.2.x-dev
- drupal/core-recommended 9.1.x-dev requires drupal/core 9.1.x-dev -> satisfiable by drupal/core(9.1.x-dev).
- Conclusion: don't install drupal/core 9.1.x-dev
- drupal/core-recommended 9.0.x-dev requires drupal/core 9.0.x-dev -> satisfiable by drupal/core(9.0.x-dev).
- Conclusion: don't install drupal/core 9.0.x-dev
- drupal/core-recommended 9.0.0-beta2 requires drupal/core 9.0.0-beta2 -> satisfiable by drupal/core(9.0.0-beta2).
- Conclusion: don't install drupal/core 9.0.0-beta2
- drupal/core-recommended 9.0.0-beta1 requires drupal/core 9.0.0-beta1 -> satisfiable by drupal/core(9.0.0-beta1).
- Conclusion: don't install drupal/core 9.0.0-beta1
- drupal/core-recommended 9.0.0-alpha2 requires drupal/core 9.0.0-alpha2 -> satisfiable by drupal/core(9.0.0-alpha2).
- Conclusion: don't install drupal/core 9.0.0-alpha2- drupal/core-recommended 9.0.0-alpha1 requires drupal/core 9.0.0-alpha1 -> satisfiable by drupal/core(9.0.0-alpha1).
- Installation request for drupal/core-recommended ^9.0.0 -> satisfiable by drupal/core-recommended(9.0.0, 9.0.0-alpha1, 9.0.0-alpha2, 9.0.0-beta1, 9.0.0-beta2, 9.0.0-beta3, 9.0.0-rc1, 9.0.1, 9.0.2, 9.0.3, 9.0.4, 9.0.5, 9.0.6, 9.0.7, 9.0.x-dev, 9.1.0-alpha1, 9.1.x-dev, 9.2.x-dev).
- Installation request for drupal/upgrade_status ^2.11 -> satisfiable by drupal/upgrade_status(2.x-dev, 2.11.0).

My composer.json file looks like this:

{
"name": "name",
"description": "decription",
"type": "project",
"license": "",
"authors": (
    {
        "name": "",
        "role": ""
    }
),
"repositories": (
    {
        "type": "composer",
        "url": "https://packages.drupal.org/8"
    }
),
"require": {
    "composer/installers": "^1.7",
    "cweagans/composer-patches": "^1.6",
    "drupal/admin_toolbar": "^2.0",
    "drupal/console": "^1.0.2",
    "drupal/core-composer-scaffold": "^9.0.0",
    "drupal/core-project-message": "^9.0.0",
    "drupal/core-recommended": "^9.0.0",
    "drupal/paragraphs": "^1.2",
    "drupal/paragraphs_edit": "^2.0@alpha",
    "drush/drush": "^10",
    "j7mbo/twitter-api-php": "^1.0",
    "vlucas/phpdotenv": "^2.4",
    "webflo/drupal-finder": "^1.0.0",
    "webmozart/path-util": "^2.3"
},
"require-dev": {
    "drupal/core-dev": "^9.0.0",
    "drupal/upgrade_status": "^2.11"
},
"conflict": {
    "drupal/drupal": "*"
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
    "sort-packages": true
},
"autoload": {
    "classmap": (
        "scripts/composer/ScriptHandler.php"
    ),
    "files": ("load.environment.php")
},
"scripts": {
    "drupal-scaffold": "DrupalComposer\DrupalScaffold\Plugin::scaffold",
    "pre-install-cmd": (
        "DrupalProject\composer\ScriptHandler::checkComposerVersion"
    ),
    "pre-update-cmd": (
        "DrupalProject\composer\ScriptHandler::checkComposerVersion"
    ),
    "post-install-cmd": (
        "DrupalProject\composer\ScriptHandler::createRequiredFiles"
    ),
    "post-update-cmd": (
        "DrupalProject\composer\ScriptHandler::createRequiredFiles"
    )
},
"extra": {
    "drupal-scaffold": {
        "locations": {
            "web-root": "web/"
        }
    },
    "installer-paths": {
        "web/core": ("type:drupal-core"),
        "web/libraries/{$name}": ("type:drupal-library"),
        "web/modules/contrib/{$name}": ("type:drupal-module"),
        "web/profiles/contrib/{$name}": ("type:drupal-profile"),
        "web/themes/contrib/{$name}": ("type:drupal-theme"),
        "drush/contrib/{$name}": ("type:drupal-drush")
    }
}

}

mysql – PHP memory size exhausted in drupal

FastCGI sent in stderr: “PHP message: PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 137612647 bytes)

Already I increase 2 times in php.ini file. But again memory size exhausted happening. And also I got following error /app/source_code_shop/shop/sites/all/modules/contrib/memcache/dmemcache.inc on line 64″ while reading response header from upstream.

We can’t fixed this solution. Is it memcache error or memcache module error or php coding problem. Please give me proper solution how can solve php memory exhaust problem in drupal.

theming – PHP memory size exhausted in drupal

FastCGI sent in stderr: “PHP message: PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 137612647 bytes)

Already I increase 2 times in php.ini file. But again memory size exhausted happening. And also I got following error /app/source_code_shop/shop/sites/all/modules/contrib/memcache/dmemcache.inc on line 64″ while reading response header from upstream.

We can’t fixed this solution. Is it memcache error or memcache module error or php coding problem. Please give me proper solution how can solve php memory exhaust problem in drupal.

Managing Drupal 9 backwards compatibility with Drupal 8

It seems (correct me if I’m wrong) that there are some classes or services that must be used in relevant cases in Drupal 9 that are not present in Drupal 8. For example, SymfonyComponentHttpKernelEventRequestEvent in place of SymfonyComponentHttpKernelEventGetResponseEvent. Same with temporary session storage and more.

This means that if these features are used in a module, the module can only be compatible with one of these core versions; but it seems like a complete waste to manage two codebases at this point, since the versions are still very similar.

What is the correct way to manage this compatibility in one module, and has this question been addressed by the Drupal 9 release team?

What is the best way to export a complete content type with fields in Drupal 8?

How can we export a complete content type, including fields, and import it into staging or production?