#StackBounty: #8 #forms #blocks Change value on validate form when I create or edit a block

Bounty: 50

I need change a value to element on form validation.

I have this code:

/**
 * Implements hook_form_FORM_alter().
 */
function MY_MODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'block_content_MY_BLOCK_form' || $form_id == 'block_content_MY_BLOCK_edit_form') {
    $form['#validate'][] = '_custom_validate';
  }
}

I want change the value of field field_test. This field is plain text.

function _custom_validate($form, FormStateInterface $form_state) {
  $form_state->setValueForElement($form['field_test'], 'changed');
}

But when I saved this block, the field field_test doesn’t change its value.

What I am doing wrong?


Get this bounty!!!

#StackBounty: #8 #forms #ajax #paragraphs Capturing the event of "add another field value" and current form state

Bounty: 50

I built a content type (journey plan) to allow users to choose steps in a cross-countries journey. The users choose a country from a menu (field_country), and put one or more cities from that country in a field_city field (which is inside the field_paragraph_city paragraph). I need the paragraph because I will later add fields for each chosen city.

The node structure is the following.

node
    |--field_country [select, cardinality = 1]
    |--field_paragraph_city [paragraph, cardinality = -1 (allowing multiple values)]
        |--field_city [select, cardinality = 1]  

When the content of the field_country field changes, using Ajax, I was able to re-populate field_city in the first instance (index = 0) of field_paragraph_city. I used $form_state->getTriggeringElement() to capture the changed value of field_country.

However, when the user clicks on Add another field_paragraph_city button and a new field_paragraph_city appears (say with index 1), I then need to populate the field_city widget, inside this new paragraph instance, using the current value of field_country. The triggering element changes from field_country to this add another button, so I was no longer able to access the current value of field_country.

I guess I need:

  • A way to know when the add another button is clicked
  • After capturing the add another event, access the current value of field_country

Am I thinking in the right direction?

This is the code in MODULENAME.module.

 function MODULENAME_form_node_journey_form_alter(&$form, FormStateInterface &$form_state) {

    $form['#attributes']['id'] = 'a-fixed-form-id';

    //get data $selected_country from the changed field_country, else from the default value (when editing the node)
    if(!empty($form_state->getTriggeringElement()['#value'])){
        $selected_country = $form_state->getTriggeringElement()['#value'];
    }  else if (!empty($form['field_country']['widget']['#default_value'])) {
      $selected_country = $form['field_country']['widget']['#default_value'];
    }

    // update $available_cities
    switch ($selected_country) {
      case 'france': $available_cities = ['paris', 'nice']; break;
      case 'zambia': $available_cities = ['lusaka', 'ndola']; break;
    }

    $form['field_paragraph_city']['widget'][0]['subform']['field_city']["widget"]['#options'] = $available_cities;
    // give fixed id for field_city to be able to access it and update it multiple times
    $form['field_paragraph_city']['widget'][0]['subform']['field_city']['#attributes']['id'] = 'field_city-fixed-id';

    // ajax setup
    $form['field_country']["widget"]['#ajax'] = array(
          'callback' => 'callback_update_cities',
          'wrapper' => $form['#attributes']['id'],
          'event' => 'change',
          'method' => 'replace',
          'progress' => ['type' => 'throbber', 'message' => t('Updating cities...'),],
        );
}

function callback_update_cities (&$form, FormStateInterface $form_state)  {
    MODULENAME_form_node_journey_form_alter($form, $form_state, $form_id);
    return $form;
}


Get this bounty!!!

#StackBounty: #views #theming #8 #ajax Does ajax used in views call preprocessing hooks?

Bounty: 50

I have a view with a bunch of fields and I’m styling those fields in:

views-view-fields.html.twig

In my .theme file I’m also using a preprocess function to pass additional variables to the above template.

function theme_preprocess_views_view_fields(&$vars) {
  $vars['my_var'] = 'hello';
}

So in the twig template, I’m able to do something like:

{% for field in fields -%}
  {{my_var}}
{%- endfor %}

This is working like a charm for the first loaded fields, but when I hit “Load More” or I use exposed filters to filter down the view, the other loaded elements will not have my_var.

Now, “kinting” my_var directly in the template file. I saw that my_var is NULL for all other elements loaded after with Ajax and this made me think that the problem is with
theme_preprocess_views_view_fields being called once, the first time the page is loaded and completely being ignored from the Ajax call.

Shouldn’t this preprocess be called and handled by the Ajax call Drupal is doing on the view itself?

If not, is there a way to query Drupal to re-run this hook when other elements are loaded?

Thanks!


Get this bounty!!!

#StackBounty: #8 #entities Entity queue JSONAPI pagination & How to add JSON API in views?

Bounty: 50

I’m using below modules

https://www.drupal.org/project/entityqueue & JSON API

Through entity queue. I have added 5 Articles.

Now I would like to apply pagination on that.

We are accessing via

http://domain.com/jsonapi/my-5-articles?include=items

I tried to add include=items&items[paging]=1. but seems not working

I need to add limit & offset

If we do same in

http://domain.com/jsonapi/node/article?page[limit]=1

then we are getting

"links": {
"self": "http://domain.com/jsonapi/node/article?page%5Blimit%5D=1",
"next": "http://domain.com/jsonapi/node/article?page%5Boffset%5D=1&page%5Blimit%5D=1",
"last": "http://domain.com/jsonapi/node/article?page%5Boffset%5D=31&page%5Blimit%5D=1"
},

We are not able to get same in entityqueue.

How to add JSON API in views?


Get this bounty!!!

#StackBounty: #8 #entities Entity queue JSONAPI pagination

Bounty: 50

I’m using below modules

https://www.drupal.org/project/entityqueue & JSON API

Through entity queue. I have added 5 Articles.

Now I would like to apply pagination on that.

We are accessing via

http://domain.com/jsonapi/my-5-articles?include=items

I tried to add include=items&items[paging]=1. but seems not working

I need to add limit & offset

If we do same in

http://domain.com/jsonapi/node/article?page[limit]=1

then we are getting

"links": {
"self": "http://domain.com/jsonapi/node/article?page%5Blimit%5D=1",
"next": "http://domain.com/jsonapi/node/article?page%5Boffset%5D=1&page%5Blimit%5D=1",
"last": "http://domain.com/jsonapi/node/article?page%5Boffset%5D=31&page%5Blimit%5D=1"
},

We are not able to get same in entityqueue


Get this bounty!!!

#StackBounty: #8 #drupal-commerce Commerce 2 Add to cart with quantity field and Product Picture

Bounty: 100

I have added an image field in product variation type. Add few products to my store. Now when I am going to product detail page to add the product in cart it just shows the add to cart button without any quantity field. Also not showing Image of the product.

I tried to change the formatter to Rendered Entity, but it then only showing the picture and price but not the add to cart button. Rendered Entity was using default view mode. So I created additional view mode through manage display of product variation type but that also didn’t provide any option to show the add to cart button.

What I want is to display the product variation picture + Price + SKU + Add to Cart button with quantity field.

Please guide me how to achieve this.


Get this bounty!!!

#StackBounty: #8 #i18n-l10n Translate blocks in page_manager variant

Bounty: 50

I am building a multilingual Drupal site. My ‘static’ pages are built using the page_manager module. I am using the core multilingual modules and the Translation Manager Tool. With these I can translate my custom entities / block / blog posts and the like.

The problem I am having is that I cannot translate the content of the blocks I have placed in my page_manger variants. If I load them up in the translation management tools only the Label of the page is available for translation, not any of the ‘content’ of the page.

I can see that I can create a new variant that is selected based on the current language, but this means I need to recreate each of the page layouts.

What I would like to do is have a single page variant but translate the content of the blocks in them in the same way that I would translate an entity or a custom block.

I cannot find a way to do this, is this possible?


Get this bounty!!!

#StackBounty: #8 #i18n-l10n #page 8 -Translate blocks in page_manager variant

Bounty: 50

I am building a multilingual Drupal site. My ‘static’ pages are built using the page_manager module. I am using the core multilingual modules and the Translation Manager Tool. With these I can translate my custom entities / block / blog posts and the like.

The problem I am having is that I cannot translate the content of the blocks I have placed in my page_manger variants. If I load them up in the translation management tools only the Label of the page is available for translation, not any of the ‘content’ of the page.

I can see that I can create a new variant that is selected based on the current language, but this means I need to recreate each of the page layouts.

What I would like to do is have a single page variant but translate the content of the blocks in them in the same way that I would translate an entity or a custom block.

I cannot find a way to do this, is this possible?


Get this bounty!!!

#StackBounty: #views #8 #charts How to programmatically set a Views display to override style options

Bounty: 50

I am working on the Charts module and have a stubborn issue that I’m trying to resolve.

Charts provides a Views style plugin and a Views display plugin called Chart Attachment. The display plugin extends the Attachment display plugin and sets the View’s format to Charts. However, I run into an issue where I really need the Chart Attachment’s formatting settings to apply to its display only (“This chart_extension (override)”). If it applies to all displays, it ends up breaking the Chart because the whole idea behind the Chart Attachment is for it to be different than its parent.

Here is what the Format settings should look like immediately after clicking “Add” -> “Chart attachment” in the View displays section:

showing the italic format section

Note the italic font on Chart and Settings within the Format section.

When you click on Chart from that attachment, this is what you should see without clicking anything else:

showing this chart_extension override

So how can one code the Display plugin to enforce style format options to “This chart_extension (override)”?

Or, if there is another approach that accomplishes the same thing, I would be open to that.


Get this bounty!!!