Drupal StackExchange

Update existing 'paragraph' entity while changing the ID is not supported

3 days 19 hours ago

I'm trying to update a paragraph with this code:

$entityManager = \Drupal::entityTypeManager(); $paragraph = $entityManager->getStorage('paragraph')->load('1'); $paragraph->set('field_test','testValue'); $paragraph->save();

But I received this error:

Drupal\Core\Entity\EntityStorageException: Update existing 'paragraph' entity while changing the ID is not supported. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 846 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Do you know why?

P.s. If I try to update with the last ID it works. It doesn't work only for old Paragraphs.

RenatoG

Unable to upload files through RESTful services

3 days 20 hours ago

I am trying to upload pdf files to webform submission using REST API.

Since the Core File Upload functionality did not seem applicable for webform submission, I applied the patch from webform_rest module which would allow uploading files for webform submissions.

When POSTing the file via /webform_rest/{webform_id}/upload/{field_name} with the necessary Content-Type and Content-Disposition, Cookie and X-CSRF-Token per documentation, the file entity response's filesize value is always zero and correspondingly the destination file is empty. The file size and extension are in accordance to the settings in webform's Document file field. The public, private and tmp directories are writable and file upload works fine when performed through the webform submission form.

I tried to check if this issue is specific to the webform_rest resource by trying the File upload resource (/file/upload/{entity_type_id}/{bundle}/{field_name}) for a specific entity type with the respective file field and observed the same result of file being empty. To try and understand where the issue might be I looked into both the Rest resource code and tried checking the symfony $request->files parameter which appears to be empty in both cases.

Since the file upload functionality works fine within the site and related php configurations like file_uploads, upload_max_filesize, post_max_size are already set I'm uncertain as to how to approach this problem further

Would request some clarity on how I could debug this further and get it to work.

optimusprime619

Change Classes in Nav-Tag of Custom Menu

3 days 21 hours ago

On a Drupal 8 installation, I have a menu in the sidebar first region. I want to add classes to bootstrap classes to the different levels of that menu. For that I created a my own menu template, which looks like this:

{# /** * @file * Theme override to display a menu. */ #} {% import _self as menus %} {{ menus.menu_links(items, attributes, 0, my_menu) }} {# 1. #} {% macro menu_links(items, attributes, menu_level, my_menu) %} {# 1. #} {% import _self as menus %} {# 1. #} {% set menu_classes = [ 'navbar-nav', 'mr-auto' ~ my_menu|clean_class, ] %} {# 1. #} {% set submenu_classes = [ 'navbar-nav', 'mr-auto' ~ my_menu|clean_class, ] %} {% if items %} {% if menu_level == 0 %} <div class="navbar-collapse offcanvas-collapse open"><ul{{ attributes.addClass(menu_classes) }}> {# 1. #} {% else %} <ul{{ attributes.removeClass(menu_classes).addClass(submenu_classes) }}> {# 1. #} {% endif %} {% for item in items %} {# 1. #} {% set item_classes = [ 'nav-item' ~ my_menu|clean_class, item.is_expanded ? 'expanded' ~ my_menu|clean_class, item.is_collapsed ? 'collapsed' ~ my_menu|clean_class, item.in_active_trail ? 'trail' ~ my_menu|clean_class, ] %} {# 1. #} {% set link_classes = [ 'nav-link' ~ my_menu|clean_class, ] %} <li{{ item.attributes.addClass(item_classes) }}>{# 1. #} {# 1. #} {{ link( item.title, item.url, item.attributes.removeClass(item_classes).addClass(link_classes) ) }} {% if item.below %} {{ menus.menu_links(item.below, attributes, menu_level + 1, my_menu) }} {# 1. #} {% endif %} </li> {% endfor %} {% if menu_level == 0 %} </ul></div> {% else %} </ul> {% endif %} {% endif %} {% endmacro %}

This works fine. However, I also need to change the classes of the nav tag. I can't seem to figure out, how that can be done and I can't find any information anywhere explaining this. So how do I change the nav tag classes of that menu?

In the end I want it to be a bootstrap offcanvas menu like this one: https://getbootstrap.com/docs/4.0/examples/offcanvas/

But I just can't seem to change the nav tag classes, since I don't know how.

TheKidsWantDjent

Controlling a dependent field in a multi-instance paragraph

3 days 22 hours ago

The fields below are part of the node/add node/edit forms of my custom node type.

The paragraph field_para_city contain one field_city. (I later need to add other fields in the paragraph).

The user can add multiple instances from field_para_city.

field_city has an allowed_values_function defined in a YML file, which returns all possible cities from all countries.

I created a class MyNodeTypeForm extends NodeForm, with a public function buildForm(). This function adds AJAX to field_country so that when it changes (i.e. the user chooses a country) it (re-)populates all the existing instances of field_city (inside the field_para_city instances) with corresponding list of cities (e.g., London and Cardiff for the UK).

In MY_MODULE.module, I added the following so that the above code is executed on the node/add and node/edit forms.

function MY_MODULE_entity_type_alter(array &$entity_types) { $handlers = $entity_types['node']->get('handlers'); $handlers['form']['default'] = 'Drupal\MY_MODULE\Form\MyNodeTypeForm'; $handlers['form']['edit'] = 'Drupal\MY_MODULE\Form\MyNodeTypeForm'; $entity_types['node']->set('handlers', $handlers); }

All works fine, except when the user add or remove instances of field_para_city ("Add city" or "Remove"), then all instances of the field_para_city are refreshed and field_city instances inside them are reset to the allowed values (all cities), ignoring the state of field_country.

The user would then need to re-select from field_country to get the correct cities again.

I found that although adding/removing a field_para_city triggered the above mentioned buildForm() function, the $form_state->getValue('field_country') was then empty, unlike when buildForm() is triggered through the field_country AJAX.

I tried to capture the state of field_country in the field_city's allowed_values_function, so that the allowed options are dynamically and directly set based on the user choice of country, but $form_state was not recognised there.

I know this is an incorrect approach!

What is the correct better approach to implement this simple use case?

Orion

Create duplicate registration form with extra fields

3 days 23 hours ago

I have a site built using Drupal 7. The site has a registration form now. There is a new requirement to add a couple of new fields in the registration form and put it in a new page.

http://example.com/register is the existing registration form and user submitting this form will be registered. http://example.com/newpage is a new page with some content and registration form with two extra fields. When users submit this page, a new user account will be created along with saving the new fields to the user profile.

How can I implement this?

Pramod Sivadas

Validation error on back button in Multistep Form

4 days ago

Validation errors with a back button on Multistep form.

I used form alter for this but this is also not working below are my code.

function mymodule_form_alter(&$form, &$form_state, $form_id) { if($form_id=='dataset_node_form') { $form['actions']['previous']['#submit'][] = array(); $form['actions']['previous']['#limit_validation_errors']=array(); } }
Fawwad

Image is "broken" with Custom Email Twig Template sending with Webform

4 days 1 hour ago

The setup consists of Drupal 8 + Webform (ver. 5.6 - https://www.drupal.org/project/webform/releases/8.x-5.6) + CKEditor Image Upload (https://www.drupal.org/project/ckeditor_uploadimage). This allows for users to copy and paste screenshots of issues into CKEditor easily.

When the user takes a screenshot and then uses CTRL+V to paste into CKEditor - the image is uploaded to the server and it works perfectly.

The problem arises when the user right clicks within CKEditor and selects "paste" from CKEditor's right click context menu - it then embeds the image directly (data uri - eg.

<img data-widget="uploadimage" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAiwAA....)

which isn't a problem, until I the email is sent.

I'm using a Twig Template for the body of the message (it's being emailed / pushed into a ticketing system, thus the need to customize the look) and the code is the following:

{% set upload_screenshot = webform_token('[webform_submission:values:upload_screenshot]', webform_submission) %} {% if upload_screenshot %} <p><strong>Screenshot(s):</strong></p><p> {{ upload_screenshot }} {# Used to try to debug why data uris weren't rendering - with webform debug it displays data uri images, but the downside is that it doesn't render regular image uploads. #} {#{{ webform_debug(upload_screenshot) }}#}</p> {% endif %}

If I use the "Default" template, any type of image (whether uploaded or as an embedded image) works fine. So I'm starting to think it's the way that webform_token processes / outputs the information.

When I inspect the code in the broken image, I notice that the data: is removed from the src

<img data-widget="uploadimage" src="image/png;base64,iVBORw0KGg )

For the meantime I'm disabling the right click context menu with this module. (https://www.drupal.org/project/ckeditor_browser_context_menu)

Any ideas on how I can get the images (whether uploaded or embedded) in a custom twig email template would be appreciated.

B.Friddy

Ubercart Node/Product Field access to Role and purchaser

4 days 2 hours ago

I have an Ubercart ecommerce Drupal 7 site I'm building. The user can either subscribe (monthly/annually) or purchase node by node (pay as you go).

I need to GRANT the subscriber or purchaser access to the main body field of the product node only, as teasers and image fields etc will be used elsewhere on the site. (This rules out a few node_access options)

I would like to combine the permissions in my own custom module, as the I can't see any contrib permissions modules that fit my needs. e.g. Node access product.

Is there an simple custom way to ALLOW access to a field after I have checked if the user has:

a) Purchased this node, or
b) is a subscriber with access (I'm using uc_recurring for ROLE assign on purchasing a subscription).

I think it could due done manually in a module checking directly for current logged in users role(s) and purchases (nid's) and seeing is the logic plays out, but will this be slow on performance when there are 1000's users and 1000's products?

Please let me know if you need any more information or if I'm on the right track.

Joe Mewes

Why does "Cancel People Account" not work?

4 days 3 hours ago

I have Drupal 7 with Devel installed running over PHP internal server and MySQL. All user operations work correctly except Admin>People>Cancel Account operation.

This leads to the overlay Add User and a Cancel Accounts button. If I select the option: Delete the account and its content and press Cancel Accounts.

This leaves the overlay(displays the home page of the user)

All I want at this point is to be in the backend Overlay and show the message Changes saved or something similar. When I return to Admin People overlay, the user is still there.

Has anyone experienced a similar problem - and do you have a solution?

Thank You. -Robert.

Robert Onslow

Query returns 0 rows on JQGrid filter due to translated rows

4 days 4 hours ago

I have a JQGrid table that can be displayed in different languages. The data for this table is stored as English text and translated when output. The JQGrid table can be filtered by text.

The issue I have, is that when the JQGrid table is filtered, the query will run against the English text. E.g.

Original table: Yes No

Translated table: Oui Non

User is French and tries to filter for 'Oui': Database query will return zero results because 'Oui' does not exist in the database table. If the French user queries for 'Yes' then Oui will show because it matches the database and is translated on output.

This is using custom integration with JQGrid.

Since all string translation is cached and as far as I know there are no database tables available to join on, how can I get translated filtering to work?

Any ideas on the best way to handle this?

Oliver P