alter query view by adding fields in an andGroup within an orGroup

1 week 1 day ago

I have a multilanguage site with two languages: English and Dutch. Not all nodes are created in both languages. Some nodes even have no language ('und', 'zxx').

I want the default Drupal provided taxonomy view output all nodes of a taxonomy-term in the current and no node-language, AND, if present, other nodes of this term from the other language. The Drupal provided sql is:

SELECT "taxonomy_index"."sticky" AS "taxonomy_index_sticky", "taxonomy_index"."created" AS "taxonomy_index_created", "node_field_data"."nid" AS "nid", "node_field_data"."langcode" AS "node_field_data_langcode" FROM {node_field_data} "node_field_data" LEFT JOIN {taxonomy_index} "taxonomy_index" ON node_field_data.nid = taxonomy_index.nid WHERE ((taxonomy_index.tid = '14')) AND (("node_field_data"."langcode" IN ('nl', 'und', 'zxx')) AND ("taxonomy_index"."status" = '1')) ORDER BY "taxonomy_index_sticky" DESC, "taxonomy_index_created" DESC LIMIT 11 OFFSET 0

I want, if applicable, extend the where clause so it becomes by example:

WHERE (((taxonomy_index.tid = '14')) AND ((("node_field_data"."langcode" IN ('en', 'und', 'zxx')) AND ("taxonomy_index"."status" = '1'))) OR (("taxonomy_index"."tid" = '14') AND ("node_field_data"."nid" IN ('43')) AND ("node_field_data"."langcode" NOT IN ('en', 'und', 'zxx'))))

I wrote a function example_views_query_alter(ViewExecutable $view, QueryPluginBase $query) in which I found out which extra nodes should be added in the extra OR clause. By adding this code:

$group_id = $query->setWhereGroup('OR'); $query->addWhere($group_id, 'taxonomy_index.tid', $tid); $query->addWhere($group_id, 'node_field_data.nid', $missing, 'IN'); $query->addWhere($group_id, 'node_field_data.langcode', $langcodes2, 'NOT IN');

But in the extended where clause I get OR instead of AND statements:

WHERE ((taxonomy_index.tid = '14')) AND ((("node_field_data"."langcode" IN ('en', 'und', 'zxx')) AND ("taxonomy_index"."status" = '1')) OR (("taxonomy_index"."tid" = '14') OR ("node_field_data"."nid" IN ('43')) OR ("node_field_data"."langcode" NOT IN ('en', 'und', 'zxx'))))

But it should be an AND group within an OR Group, maybe like:

$query->setWhereGroup('OR'); $query->setWhereGroup('AND'); $query->addWhere(...

Who can help me out to achive this?

PROMES

Providing menu route parameter values from url

1 week 1 day ago

Suppose I have the following route in my ej.routing.yml file:

ej.form.user.profile: path: '/user/{user}/ej_profile' defaults: _title: 'EJ Profile' _form: '\Drupal\ej\Form\User\ProfileForm' requirements: _role: 'authenticated' user: \d+

Now I want to add a menu based on the above route to Tools menu. So I use the following in ej.links.menu.yml file:

ej.user.profile: title: 'EJ Profile' description: 'EJ Profile' route_name: ej.form.user.profile menu_name: tools weight: 100

However this gives me an exception saying: Symfony\Component\Routing\Exception\MissingMandatoryParametersException: Some mandatory parameters are missing ("user") to generate a URL for route "ej.form.user.profile".

The same works as a task entry. Basically I have a custom form that extends user profile to integrate with a third-party system and I need the user id from the url to fetch appropriate user's information.

How can I have the menu to pick route parameter from the url?

r_honey

How to delete children when main entity is deleted?

1 week 2 days ago

I am looking for the best way to delete children of my main entity when i delete it.

I have 2 entity type :

commandes_entity

commandes_ligne_entity ( entity reference to commandes_entity )

The only way i have found is to create a hook_entity_delete :

Edit : This is how i delete children :

function boutique_commandes_entity_delete(Drupal\Core\Entity\EntityInterface $entity) { $query = \Drupal::database()->delete('commandes_lignes_entity'); $query->condition('commandes_id', $entity->get('id')->value); $query->execute(); }

Is there another way (maybe better) for delete children ?

Kevin

Unable to edit node translation other than current language

1 week 2 days ago

On my multilingual site I seem to have problem at admin/content when editing a particular translation.

I have a Contact Us page, translated:

Contact Us [EN], node/286 Contact Us [ES], node/300

I don't have an explicit Path Translation set, but the nodes are bound together, in the Translate tab.

Now the problem is, that when my current language is English and I want to edit the Spanish version from the Content listing, the edit link takes me to its English version (node/286). Apparently the admin overlay inherits the language prefix and forces the English translations to open.

What luckily works correctly is editing the node from within its view (by hitting the Edit tab).

Is this normal behavior?

no more drupal

Using Twig in global custom text to check for a specific content type

1 week 2 days ago

I am filtering for two content types to display in a view: Artwork Page and Cover Page. I want to change the HTML markup for Cover Page.

I have researched and tried many field names for Content Type, but with none of them I get what I am trying to achieve. In this instance, the replacement pattern for Content Type is {{ type }}; using it doesn't work. I've tried both the machine name (cover_page) and the name (Cover Page), but neither of them worked.

{% if type == 'cover_page' %} <h1>THIS IS THE BEGINNING</h1> {% endif %} {% if type == 'Cover Page' %} <h1>THIS IS THE BEGINNING</h1> {% endif %}
David Crellen

How can I add a class to a label?

1 week 2 days ago

I need to add a class name to certain labels created by Drupal's form API like this:

$form['name'] => array( '#type' => 'textfield', '#title' => 'Prénom' );

If I use the following, the <textarea> gets a class, but not the label.

$form['name']['#attributes']['class'] = array('myClass');

I'm looking for something similar that will add a class to the <label>.

Shawn

Get web image url from real path

1 week 2 days ago

As part of some other functionality I am generating an image in temp folder and But unforunately I'm not able get the web url of image.

$path = \Drupal::service('file_system')->realpath(file_default_scheme() . "://"); $image_path = $path . rand() . '.png'; echo $image_path;

I am getting following as path

C:\wamp\www\www_d8test1_com\sites\default\files30605.png

How can I add slash before image name and get actual weburl of image like below?

http://www.d8test1.com/sites/default/files/30605.png
Cool

How do I use Taxonomy reference field tokens in a path alias?

1 week 2 days ago

I have a content type with two taxonomy entity reference fields. Both are required fields that only allow one value. I have path_auto and token enabled. I would like to use them to set the path of the node type.

This is the path pattern that is a problem:

[node:field_main_site_section:entity:name]/[node:field_content_category:entity:name]/[node:title]

It looks close to the answer given in another questionn here.

Result: The only thing showing up in the path is the title.

Versions: Drupal 8.8.5, pathauto 1.8, token 1.7

What have I tried?

I tried running the code through the debugger and found out that the action is in the functions PathautoGenerator::createEntityAlias() and Token::generate(). I see that the three tokens are found and that a Node data is passed to Token generate. Then the generate function runs $replacements = $this->moduleHandler->invokeAll('tokens', [$type, $tokens, $data, $options, $bubbleable_metadata]). There were 9 modules found that implement hook_tokens but the only one returning any data is node_tokens. Looking at that code I don't see any attempt to return fields.

I also found an issue on the token module that indicated that I might be missing Token view modes. I was indeed missing a token view mode for my two taxonomy vocabularies so I created them. I added each field in the vocabulary including "name" in plain text.

I also tried to guess that the node needed to be converted to an entity to grab it's fields:

[node:entity:field_main_site_section:entity:name]/[node:entity:field_content_category:entity:name]/[node:title]

That was an invalid pattern.

Question: What is the correct pattern or what else do I need to configure?

lolcode

Calendar From Multiple Content Types

1 week 2 days ago

I have a 4 seperate calendars created on drupal; each with their own content type. Each of these need to be able to be viewed on their own but I'd also like to be able to combine all of these calendars into one master view and can't seem to figure it out.

ie. Meetings calendar, Holidays calendar, Staff Events calendar.

Any help would be greatly appreciated. I'm new to Drupal so please be gentle.

Thanks

user697508

Nivo Slider jQuery plugin Not installed

1 week 2 days ago

I´m trying to install Nivo Slider to my website. I was following this tutorial from the official plugin website.

I have the folder "all" an inside it I have "libraries", "modules", and "themes". I extracted the FlexSlider-master.zip in my libraries folder, and rename it to "nivo-slider".

The second step was install the extension from the url drupal/project/nivo_slide without drush, download the folder an upload to my server in modules/.

I have clear the cache but the problem persist.

Can someone help me? I can´t upload pictures or links with less than 10 reputation.

Leonardo Cavani

main menu link as language switcher

1 week 2 days ago

I have 2 language and these situation:

  • main menu -> lang1 -> link to www.foo.com/
  • main menu -> lang2 -> link to www.foo.com/en

All sites when switch manually on browser the language, But my main menu link not work.

I have no space to place an other block with menu switcher on main menu bar.

I hope someone can send me in right direction because I spent 8 hour around these without solutions?

theman whosoldtheworld

Entity reference field hidden widget or edit disabled on custom form

1 week 2 days ago

I have a custom node creation form I am rendering within a views preprocess hook. In that form I have two entity reference fields. I am setting the [#value] and [#default_value] of those reference fields programmatically.

On form submit the values I've set are created, but only if the fields are "editable" by the user. I want to make the fields "disabled" and/or "hidden" from the user. Not just hidden with CSS but with the entire autocomplete widget being made inaccessible. The field needs to be type "hidden".

How can I make an autocomplete entity reference disabled and/or inaccessible to the user creating the content?

Or another way of putting it, how can I add entity reference values programmatically on form submit without the user being able to interact with the fields?

Here are some things I've tried that do not work:

  1. $form['field_entity_reference']['#access'] = false; // Setting #access to false seems as though it should work because the form fields still exist when debugging $form, but it does not work as it prevents the value from being submitted.
  2. Adding ['#attributes']['readonly'], changing ['#type'] to hidden or another widget type, adding ['#attributed']['disabled']. None work
  3. Entity prepopulate - This does not work for this particular situation as some values cannot be provided by tokens

Code for reference

function <theme>_preprocess_views_view_field(&$variables) { $view = $variables['view']; if ($view->id() == '<view>') { $field = $variables['field']; switch($field->options['id']) { case '<view_field>': $node = \Drupal::entityTypeManager() ->getStorage('node') ->create(['type' => '<node_type>']); $form = \Drupal::service('entity.form_builder')->getForm($node, 'secondary'); // Set default_value and value $form['field_entity_reference']['widget']['#default_value'] = (int)$cid; // Note this is using the "select list" widget for the entity reference field $form['field_entity_reference']['widget']['#value'] = (int)$cid; // Same as above // Stuff that doesn't work. Values do not submit unless field is editable by user, or the field is not hidden/disabled $form['field_entity_reference']['#access'] = false; // Value does not submit $form['field_entity_reference']['widget'][0]['#attributes']['readonly'] = 'readonly'; // Doesn't make field read only $form['field_entity_reference']['widget'][0]['value']['#type'] = 'hidden'; // Seems autocomplete widget and select widget do not have a "hidden" type $form['field_entity_reference']['widget'][0]['target_id']['#type'] = 'textfield'; // Seems one cannot change the type to textfield // Change view field output to the form $variables['output'] = $form; break; } } }
Jason

How to conditionally display some things in the message text created by the Message module?

1 week 2 days ago

I am using the Message module to record some activity.

I have a Rule generating a Message. The Message Type has a couple of fields, and I am setting them in the rule. That part all works great. I can verify the contents of each of those message fields.

Now what I'm trying to do is to conditionally display some things in the message text. I have a couple of Message Text rows and they all display what I expect them to. But for one of these, I want only want to display something if one of the tokens matches a condition.

I have selected the PHP Code filter type on that particular Message Text box, but I just don't know to actually evaluate the field values of the current message. I've tried t() and token_replace() and I just am not getting it.

So here is a simple question for this problem I'm having. Say I have a text field on this message type called field_foo. And I want the message text to display the length of that field. I basically want something to the effect of <?php strlen(this-message:field_foo) ?>.

How do I reference my message instance's field?

franksj

Get the uri/path value of an image in the field template

1 week 2 days ago

I have created a custom field template for an image - field--field_images.tpl.php with the following code:

<?php $url = file_create_url($field_images['uri']); $url = parse_url($url); $imglink = $url['path']; ?> <div id="slideshow"> <?php foreach ($items as $delta => $item): ?> <div data-thumb="<?php print render($imglink); ?>" data-src="<?php print render($imglink); ?>" data-time="1500" data-trasPeriod="4000" data-target="_blank" ></div> <?php endforeach; ?> </div>

currently $imglink returns the value of the root folder "www". I need to find a way to get the path to the image file associated with the field e.g "http://www.example.com/sites/default/files/image.jpg" any help with this would be greatly appreciated.

UPDATE:

I think I'm almost there. Here is the code in my field-field_images.tpl.php file:

<?php foreach ($items as $delta => $item): ?> <div data-thumb="<?php $img_src = $element['#object']->field_images['und'][0]['uri']; $link = image_style_url('thumbnail', $img_src); print ($link); ?>" data-src="<?php $img_src = $element['#object']->field_images['und'][0]['uri']; $link = file_create_url($img_src); print ($link); ?>" data-time="1500" data-trasPeriod="4000" data-target="_blank" ><?php render($item); ?></div> <?php endforeach; ?>

and you can see the output here: http://www.kfpa.co.za/dev/?q=node/2

The one problem is that it loops through and creates the correct number of elements for the uploaded images, but when it creates the URLs it's only creating for the item as specified for by 'x' in <?php $img_src = $element['#object']->field_images['und'][x]['uri']; i.e if I change 'x' to '2' the third photo displays. How do I change this code so that it finds the correct URL for corresponding image in the loop?

flamesquirrel

Database error with migration

1 week 2 days ago

I am trying to migrate users and a few other entities from a Drupal 7 site to Drupal 8 with the built-in migration modules and Migrate Plus. I have added a database array in settings.php but something is missing to get the migrations to "see" the right database. I am getting this error.

[Mon May 11 22:22:56.025896 2020] [php7:error] [pid 6389:tid 140003403929344] [client 62419] PHP Fatal error: Method Drupal\user\Plugin\migrate\source\d7\Role::__toString() must not throw an exception, caught Drupal\migrate\Exception\RequirementsException: No database connection configured for source plugin d7_user_role in /var/www//web/core/lib/Drupal/Component/Utility/Unicode.php on line 595, referer:

I have tried adding

shared_configuration: source: key: database_key

But it did not work.

Any help is greatly appreciated.

Thanks, Josh

Josh

Get display mode of product or product variation where an 'add to cart form' is used in hook_form_alter for the add_to_card_form

1 week 2 days ago

I'm using Drupal 10 and the commerce module. I'm using a product display mode 'card' that uses the variations 'add to cart form' format. In hook_form_alter I am hiding the quantity field:

function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { if (strpos($form_id, 'commerce_order_item_add_to_cart_form_commerce_product_') === 0) { $form['quantity']['widget'][0]['value']['#type'] = 'hidden'; } }

This works fine, but the problem is that it is obviously hiding the field for every view mode. I would like to only hide it for a my 'card' view mode. How can I get the display view mode of the product (or product variation) where the the 'add to cart form' is used.

Mike

Change the Pay button text

1 week 2 days ago

I have created Drupal 9 website. On the website we accept donations using Commerce with Commerce Stripe as the payment processor. By default, the payment button says "Pay and complete purchase". As users are not purchasing anything, I want to change the text to something like "Donate Now". I have searched for answers online but all seem to point to the module String Overrides, which is D7 only and doesn't look like is will have a D8/9 option.

Any idea how to change the text in Drupal 9?

FrankDesign
Checked
11 hours 1 minute ago
most recent 30 from drupal.stackexchange.com
Subscribe to Drupal StackExchange feed