Exception https://blog.werk21.de/en en How to "fix" 403 access denied exceptions for Drupal 8 Views translation routes https://blog.werk21.de/en/2018/12/06/how-fix-403-access-denied-exceptions-drupal-8-views-translation-routes <span>How to &quot;fix&quot; 403 access denied exceptions for Drupal 8 Views translation routes</span> <span><span lang="" about="/en/user/8" typeof="schema:Person" property="schema:name" datatype="">patrick</span></span> <span>Thu, 12/06/2018 - 08:50</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>Today I worked on a project where the customer had asked us to add a missing translation for the label of an exposed filter of a view. The view configuration was originally imported upon installation of a contributed module, that uses that view. I thought that this would be an easy task quickly done: We have configuration translation enabled on that system so all views have a "Translate" operation that you can use to translate the view. Not in this case, of course ;).<br /> <br /> I was logged in as the magic super user 1 with the magic administrator role assigned to it, but I still got a 403 access denied exception each time I tried to translate this particular view. Other views that we had created manually or that were provided by other contribued modules worked fine. But not this one. What the ****?<br /> <br /> After searching the core issue queue for a while to no avail, I ended up to comment out the "throw AccessDeniedException" in the AccessAwareRouter, just out of curiosity. After all this was a dev system, so what could possibly go wrong? And then I instantly realized what the problem was. The list of translations was displayed and I saw that the "original translation" was "language undefined" or "und". Looking at the configuration object more closely confirmed, that the language was "und". In hindsight, it was kind of logical that I couldn't translate that one, right?<br /> <br /> Of course I didn't make the foolish attempt to create an English translation for the view via UI. Since Views doesn't have a UI to change the language of a view like for example Content entities have, I decided to just export the configuration using Drush, find the YAML file for the configuration object in question, edit it so that the language was no longer "und", but "en", and import the configuration again using Drush. Now I was able to translate the view using the UI without my quick modification of the AccessAwareRouter.<br /> <br /> Yet another lesson learned.</p></div> <div class="field field--name-field-systems field--type-entity-reference field--label-inline"> <div class="field--label">Systems</div> <a href="/de/systems/drupal-8" class="label label-default" hreflang="de">Drupal 8</a> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <a href="/de/tags/drush" class="label label-default" hreflang="de">drush</a> <a href="/de/tags/views" class="label label-default" hreflang="de">Views</a> <a href="/en/tags/translation" class="label label-default" hreflang="en">Translation</a> <a href="/en/tags/config-translation" class="label label-default" hreflang="en">Config Translation</a> <a href="/en/tags/yml" class="label label-default" hreflang="en">yml</a> <a href="/en/tags/accessawarerouter" class="label label-default" hreflang="en">AccessAwareRouter</a> <a href="/en/tags/accessdeniedexception" class="label label-default" hreflang="en">AccessDeniedException</a> <a href="/en/tags/exception" class="label label-default" hreflang="en">Exception</a> </div> Thu, 06 Dec 2018 07:50:41 +0000 patrick 148 at https://blog.werk21.de Illegal mix of collations exception caused by Drupal 8 Autocomplete with Entity Reference View https://blog.werk21.de/en/2017/09/08/illegal-mix-collations-exception-caused-drupal-8-autocomplete-entity-reference-view <span>Illegal mix of collations exception caused by Drupal 8 Autocomplete with Entity Reference View</span> <span><span lang="" about="/en/user/8" typeof="schema:Person" property="schema:name" datatype="">patrick</span></span> <span>Fri, 09/08/2017 - 11:48</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>Looking at a <em>Recent log messages</em> report of one of our Drupal 8 instaces, I came across this error message:</p> <blockquote> <p><em>Drupal\Core\Database\DatabaseExceptionWrapper</em>: Exception in Media Autocomplete[media_autocomplete]: SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (ascii_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation 'like': SELECT media_field_data.langcode AS media_field_data_langcode, media_field_data.mid AS mid, media_field_data.name AS media_field_data_name, media_field_data.bundle AS media_field_data_bundle FROM {media_field_data} media_field_data WHERE ((media_field_data.bundle = :db_condition_placeholder_0) AND ((media_field_data.name LIKE :db_condition_placeholder_1 ESCAPE '\\') OR (media_field_data.bundle LIKE :db_condition_placeholder_2 ESCAPE '\\'))) AND ((media_field_data.status = :db_condition_placeholder_3) AND (media_field_data.langcode IN (:db_condition_placeholder_4, :db_condition_placeholder_5, :db_condition_placeholder_6))) LIMIT 10 OFFSET 0; Array ( [:db_condition_placeholder_0] =&gt; image [:db_condition_placeholder_1] =&gt; %gläsernes labor% [:db_condition_placeholder_2] =&gt; %gläsernes labor% [:db_condition_placeholder_3] =&gt; 1 [:db_condition_placeholder_4] =&gt; de [:db_condition_placeholder_5] =&gt; und [:db_condition_placeholder_6] =&gt; zxx ) in <em>Drupal\views\Plugin\views\query\Sql-&gt;execute()</em> (line <em>1488</em> of <em>core/modules/views/src/Plugin/views/query/Sql.php</em>).</p> </blockquote> <p>Clearly, an editor was trying to use the autocomplete functionality of an entity reference field searching for media entities matching the search string "gläsernes labor" (in English: <em>transparent laboratory</em>). This would actually happen for any search string entered into the autocomplete that didn't contain ascii characters only, so in this case the "ä" in "gläsernes" was causing the exception. Looking closer at the query, we can see that the MySQL table is "media_field_data" and the search string is in two placeholders: "db_condition_placeholder_1" and "db_condition_placeholder_2". These are used to check for matches in "media_field_data.name" or "media_field_data.bundle" columns. So I asked the database for the collation of these fields:</p> <div class="geshifilter"><pre class="php geshifilter-php" style="font-family:monospace;">MariaDB <span style="color: #009900;">&#91;</span>my_drupal_8_database<span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span> SELECT TABLE_NAME<span style="color: #339933;">,</span> COLUMN_NAME<span style="color: #339933;">,</span> COLLATION_NAME FROM INFORMATION_SCHEMA<span style="color: #339933;">.</span>COLUMNS WHERE TABLE_SCHEMA<span style="color: #339933;">=</span><span style="color: #0000ff;">'my_drupal_8_database'</span> AND TABLE_NAME<span style="color: #339933;">=</span><span style="color: #0000ff;">'media_field_data'</span> AND COLUMN_NAME IN <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bundle'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #339933;">+------------------+-------------+--------------------+</span> <span style="color: #339933;">|</span> TABLE_NAME <span style="color: #339933;">|</span> COLUMN_NAME <span style="color: #339933;">|</span> COLLATION_NAME <span style="color: #339933;">|</span> <span style="color: #339933;">+------------------+-------------+--------------------+</span> <span style="color: #339933;">|</span> media_field_data <span style="color: #339933;">|</span> bundle <span style="color: #339933;">|</span> ascii_general_ci <span style="color: #339933;">|</span> <span style="color: #339933;">|</span> media_field_data <span style="color: #339933;">|</span> name <span style="color: #339933;">|</span> utf8mb4_general_ci <span style="color: #339933;">|</span> <span style="color: #339933;">+------------------+-------------+--------------------+</span></pre></div> <p>Tada! The bundle column of the table uses ascii collation. For our autocomplete search to work, those two columns would need to have the same collation or we would need to change the query to specify the collation to use for each like condition separately. Another option would be to just query the name field or the bundle field.</p> <p>But why was the bundle field searched in the first place? We set up an entity reference view for this entity reference field (after applying a <a href="https://www.drupal.org/node/2174633">patch</a>, mind you ;). We did this because we wanted to show the bundle and some other stuff along the label of the entity in the results returned by the autocomplete. When we set up the view we selected "Name" and "Bundle" for the "Search fields" setting of the "Entity reference list" style plugin.</p> <p>Our client didn't really care that much about searching the bundle field, so I just removed it from the search fields. And voilá, you could enter strings with umlauts into the autocomplete again.</p></div> <div class="field field--name-field-systems field--type-entity-reference field--label-inline"> <div class="field--label">Systems</div> <a href="/de/systems/drupal-8" class="label label-default" hreflang="de">Drupal 8</a> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <a href="/en/tags/autocomplete" class="label label-default" hreflang="en">Autocomplete</a> <a href="/de/tags/views" class="label label-default" hreflang="de">Views</a> <a href="/en/tags/entity-reference-field" class="label label-default" hreflang="en">Entity Reference Field</a> <a href="/de/tags/mysql" class="label label-default" hreflang="de">mysql</a> <a href="/en/tags/exception" class="label label-default" hreflang="en">Exception</a> </div> Fri, 08 Sep 2017 09:48:14 +0000 patrick 127 at https://blog.werk21.de