Drupal StackExchange

AJAX form submit error when sending form to twig in hook_preprocess_page

3 hours 20 minutes ago

I have a simple AJAX form that I can't submit when the form is passed from module to the template in hook_preprocess_page. The same form works fine if sent from controller, it's just that the layout is complicated and I prefer to write the HTML part in the twig file and integrate the form where it fits best.

The form class:

<?php namespace Drupal\mymodule\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; class SubmissionForm extends FormBase { public function getFormId() { return 'mymodule_submission_form'; } public function buildForm(array $form, FormStateInterface $form_state) { $form['#prefix'] = '<div id="submission-form-wrapper">'; $form['#suffix'] = '</div>'; $form['mail'] = array( '#title' => 'E-mail', '#type' => 'textfield', '#required' => TRUE, ); $form['actions'] = array( '#type' => 'actions' ); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => 'Submit', '#attributes' => array( 'class' => array( 'btn', 'btn-success', 'use-ajax-submit' ), ), '#ajax' => array( 'wrapper' => 'submission-form-wrapper', ) ); return $form; } public function validateForm(array &$form, FormStateInterface $form_state) { } public function submitForm(array &$form, FormStateInterface $form_state) { } }

The preprocess function:

/** * Implements hook_preprocess_HOOK(). */ function mymodule_preprocess_page(&$vars) { if (\Drupal::service('path.matcher')->isFrontPage()) { $vars['page']['submission_form'] = \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\SubmissionForm'); } }

The error:

TypeError: Argument 1 passed to Drupal\Core\Ajax\AjaxResponse::setAttachments() must be of the type array, null given, called in /Volumes/Work/Git/myproject/public_html/core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php on line 56 in Drupal\Core\Ajax\AjaxResponse->setAttachments() (line 37 of /Volumes/Work/Git/myproject/public_html/core/lib/Drupal/Core/Render/AttachmentsTrait.php).

  • it's a fresh Drupal 8.7.10
  • devel and kint as contrib modules enabled
  • core inline form errors enabled
  • the form has only one required textfield and the submit button

Any suggestions on how to fix this?

Thank you!

Trudog

How do I programmatically create a view in a custom module?

4 hours 20 minutes ago

I currently have a custom module that lets you create an entity. The list page for this has gotten to be really long so I decided to create a view for this to allow filtering. If this were a new module that I was about to install then I could just throw the .yml file into config/staging, but since it has to be done as an update I cannot just put the .yml file there.

It looks like it needs to be created all in code in an update hook.

Is there an easier way of doing this?

Rudi Shafakian

Get locations search: Show all markers by default

4 hours 22 minutes ago

I played around with the get locations module for drupal escpecially with the get locations search part.

Finally I got the thing working but I'm wondering now if it's possible to display by default all markers on the map and not only the result markers after a search.

A blank map till I searched something is very confusing to the user.

Thx for any advice in advance.

EDIT

Okay I try to write a patch on my own. My research is based on version 7.x-1.10 The essential part ist to load the lat/longs of the location nodes and also the min- / maxes into the array in getlocations_search.module line 290:291 which is

$minmaxes = ''; $latlons = array();

For the "normal" map they are loaded in the function getlocations_nids() on line 828 in the file getlocations.module.

TiMESPLiNTER

Email formatting for a file field?

4 hours 44 minutes ago

I have a webform with a managed file field. I need to send that field in the email as well. I have added the submission:values field, but it doesn't add spaces between the filename and the file size.

I need to add a space between the file name and its size. How can I achieve this?

Usman Manzoor

Migrate order billing profile

4 hours 47 minutes ago

I'm trying to migrate order data from an existing site using CSV files and I can't get the billing profile to relate.

In trying to troubleshoot this, I have it set just to insert a default value of a known profile and it's still not working for me. Currently, in the process section of my YML migration file I have these lines, but I still end up with <null> in the database.

billing_profile/target_id: - plugin: default_value default_value: 34338 strict: true

I have also tried billing_profile__target_id and billing_profile/0/target_id instead of billing_profile/target_id.

What am I missing?

Delford Chaffin

Insert Record Using db_insert in Block

6 hours 18 minutes ago

I have created a block whose content uses the PHP as input filter. What I want to do is inserting a record into a separate database.

The code I am using is the following one.

$tracker = array( 'database' => 'tracker', 'username' => 'tracker', 'password' => 'PASSWORD', 'host' => 'localhost', 'driver' => 'mysql', ); Database::addConnectionInfo('tracker', 'default', $tracker); db_set_active('tracker'); $time = microtime(); $time = explode(' ', $time); $time = $time[1] + $time[0]; $finish = $time; $total_time = round(($finish - $start), 4); $t = $total_time; $url = $_GET['url']; $current_ip = $_SERVER['REMOTE_ADDR']; $user_agent = $_SERVER['HTTP_USER_AGENT']; $date = date("Y-m-d"); $time = date("h:i:s"); function is_bot() { $botlist = array( "Teoma", "alexa", "froogle", "Gigabot", "inktomi", "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory", "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot", "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp", "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz", "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot", "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler", "TweetmemeBot", "Butterfly", "Twitturls", "Me.dium", "Twiceler" ); foreach ($botlist as $bot) { if(strpos($_SERVER['HTTP_USER_AGENT'], $bot) !== false) return true; } return false; } } if (is_bot()) { $isbot = 1; } else { $isbot = 0; } db_insert('ap_visits') ->fields(array( 'id' => NULL, 'length' => $t, 'url' => '$url', 'current_ip' => '$current_ip', 'user_agent' => '$user_agent', 'date_start' => date("Y-m-d H:i:s"), 'is_bot' => $isbot, )) ->execute(); db_set_active();

When executed, the code returns the following error.

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'c1tracker.semaphore' doesn't exist' in /var/www/clients/client1/web2/web/includes/database/database.inc:2168
Stack trace:
#0 /var/www/clients/client1/web2/web/includes/database/database.inc(2168): PDOStatement->execute(Array)
#1 /var/www/clients/client1/web2/web/includes/database/database.inc(680): DatabaseStatementBase->execute(Array, Array)
#2 /var/www/clients/client1/web2/web/includes/database/database.inc(2347): DatabaseConnection->query('SELECT expire, ...', Array, Array)
#3 /var/www/clients/client1/web2/web/includes/lock.inc(167): db_query('SELECT expire, ...', Array)
#4 /var/www/clients/client1/web2/web/includes/lock.inc(146): lock_may_be_available('schema:runtime:...')
#5 /var/www/clients/client1/web2/web/includes/bootstrap.inc(420): lock_acquire('schema:runtime:...')
#6 /var/www/clients/client1/web2/web/includes/bootstrap.inc(442): DrupalCacheArray->set(Array)
#7 [internal function]: DrupalCache in /var/www/clients/client1/web2/web/includes/database/database.inc on line 2168
PDOException: SQLSTATE[HY000]: General error: 1364 Field 'is_logout' doesn't have a default value: INSERT INTO {ap_visits} (id, length, url, current_ip, user_agent, date_start, is_bot) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array
( [:db_insert_placeholder_0] => [:db_insert_placeholder_1] => 1375693838.8169 [:db_insert_placeholder_2] => $url [:db_insert_placeholder_3] => $current_ip [:db_insert_placeholder_4] => $user_agent [:db_insert_placeholder_5] => 2013-08-05 17:10:38 [:db_insert_placeholder_6] => 0 )
in eval() (line 61 of /var/www/clients/client1/web2/web/modules/php/php.module(80) : eval()'d code).

It seems that the code is trying to access 'semaphore' table when using the external database.

How can I fix that error?

jaypabs

Migrating to XAMPP local install - Configure the temporary directory

7 hours 21 minutes ago

I have copied a site I have on a VPS down to run from XAMPP on my local machine. When I restore the site using Backup and Migrate I get the error message on all pages of the site:

The specified file temporary://filemy1oSK could not be copied, because the destination directory is not properly configured.

I have clean installs of drupal running fine on XAMPP. On the File System configuration page (admin/config/media/file-system) they use '/Applications/XAMPP/xamppfiles/temp/' so I have tried using this and '/Applications/XAMPP/xamppfiles/private/' but this doesn't fix the error.

There is a similar Drupal Answers Question, about moving a site from local up to a VPS I have tried to work through the suggestions there to no effect. The top answer recommends advice from this site. Following this to avoid starting the file paths with a forward slash I tried using 'sites/default/files/private/' and 'sites/default/files/temp/'but this doesn't fix the error either.

As this is a local install, security is less of an issue. I have been setting the above directories to 777 to ensure that there are enough permissions. I have also made 'sites', 'sites/default' and 'sites/default/files/' 777.

Possibly incidentally, clean URLS weren't working on the the local install, even though they work fine on the fresh installs of Drupal. This stopped the links on the site working. I disabled them by going to http://localhost/sitename/?q=admin/config/search/clean-urls and the site links now work without clean URLS

Also possibly incidentally, I got an error message when I used the site files I downloaded to install with a clean database. I did this so I could install the backup migrate module and restore the site properly. At the end of the install I got the error:

Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in _acquia_profile_create_node() (line 158 of /Applications/XAMPP/xamppfiles/htdocs/sitename/profiles/acquia/acquia.profile).

I don't have any admin menu and the overlay admin screens aren't working. I am assuming this is a result of the temporary files error.

Any advice?

Paul Trotter

How to add markup inside a form radio button div

8 hours 23 minutes ago

Currently I have a form displaying radio buttons and I added some markup to it by attaching #suffix to the form element. However, I found that this didn't do exactly what I needed. This created the following code structure:

<div class='field-wrapper'> <div class='radio-button-group-wrapper> ... <div class='radio-button-group-inner-wrapper'> <div class='radio-button'> <input type='radio'></input> <label></label> </div> <div class='radio-button'> <input type='radio'></input> <label></label> </div> </div> </div> <div>Markup goes here</div> </div>

I'm trying to figure out how to add markup inside the 'radio-button' div, right after the label tag. How can I do this? If there is no Drupal way of doing it, I can add it with javascript, but that's not ideal.

EDIT: I have chosen to try using a custom template for this form element, but its not working. Here is my code:

'my_module_is_member_radios' => array( 'variables' => array( 'description' => array(), ), 'template' => 'templates/my_module_is_member_radios', ),
user1015214

Exposed filter value into the session and cookie?

9 hours 23 minutes ago

I want users visit my site regularly. And I need to store their settings to session (cookie). These settings are: Views Exposed Filter on the FrontPage (like to hide materials from the black list users).

So I have the FrontPage View and Taxonomy Term View. These views have exposed filter.

Once visitor selected values in this filter they should to store during he browsing the site and more when he visit site again.

I do not want to use JavaScript. I want to store cookie with php and to get it with php.

And I want drupal cache all pages for all possible filter values.

Please, have I to write a new Drupal CMS or where have I to get the answer?

tlito