8 – Automated Test Strategy for Twig Models

I'm using Drupal 8 CMS for a multimedia platform, which basically uses the Twig template creation engine. We use a component approach, building small, reusable building blocks instead of creating the complete page.

Now each component has its twig file and style sheet for the CSS rules.

My question is: which testing strategy is best for automating the twig model testing strategy?

  • Unit tests
  • Functional tests

Looks like phpunit has a way to test closed twig models https://stackoverflow.com/questions/17026405/twig-template-unit-testing

But for me, it seems excessive to do unit tests for the modeling engine, because twig models are not supposed to have a complex business logic, but yes, it could be simple if, if not, the creation of conditional branches on the model.

The functional test seems to be the best candidate for this type of assertions, but yes, the functional tests are slow because they have an opinion on the connections to the database, unlike the unit tests.

Let me re-emphasize my question for a multimedia platform using Drupal 8 as a content management system (CMS). Which testing strategy is best for testing frontal twig models? Is

  • Unit tests
  • Functional tests

I was not sure where this question fit better https://sqa.stackexchange.com/questions/39546/testing-strategy-for-twig-template, I will close one based on answers and suggestions.

theming – I want to build a multidimensional array at Twig

I want to build a multidimensional array at Twig.

In php

    $ attributes['attributes']['class']    = table ();
$ attributes['attributes']['class'][]    = & # 39; innerTab & # 39 ;;
$ attributes['html'] = TRUE;

Same thing I want to build in Twig.
I tried it but I did not have one. Please check my code below

    {% set ctn = 0%}
{% for key, value in menu.menu%}
{% set attributes = {attributes:} class}%}
{% set attributes = attributes | merge ( [ { (ctn) :'innerTab'} ,  {'html' : true} ]  )%}
{% set ctn = ctn + 1%}
{% endfor%}

As a result, I receive:

table (3) { [“attributes”]=> string (5) "class" [0]=> table (1) { [0]=>
string (8) "innerTab"} [1]=> table (1) { [“html”]=> bool (true)}}
table (3) { [“attributes”]=> string (5) "class" [0]=> table (1) { [1]=>
string (8) "innerTab"} [1]=> table (1) { [“html”]=> bool (true)}}
table (3) { [“attributes”]=> string (5) "class" [0]=> table (1) { [2]=>
string (8) "innerTab"} [1]=> table (1) { [“html”]=> bool (true)}}

8 – print data in a twig file from the custom form, via the controller

When you define the rebuild in a submission form, no controller is involved. The next page displayed is the rebuilt form, where you can print the my_report twig template by adding it to the build board. $ form.

You do not need to transfer the values ​​to $ form_state because they are available when the form is rebuilt. You can check in the form generation function if these values ​​are present. Otherwise, you create a new form. If they are, it is a form rebuild, and then display the result template:

        public function buildForm ($ form array, FormStateInterface $ form_state) {

// form of construction

if ($ form_state-> hasValue (& # 39;))) {
$ form['my_report'] = [
        '#theme' => 'my_report',
        '#role' => $form_state->getValue('role'),
      ];
}
return $ form;
}

I want you to go out with pig until sex 4987

BlackHatKings: Proxy Lists
Posted by: Afterbarbag
Post time: May 29, 2019 at 9:25.

8 – How to print the relative URL or the node ID for each node referenced in twig?

I use paragraphs to make a number of nodes on a page. Nodes are obtained via an entity reference field called "field_nodeslist". in the type of paragraph my cards. The cards are then rendered with the help of a custom view layout.

Here is an excerpt from my layout:


   

{{content.top_region | StripTags}}

{{}} Content.middle_region

The problem I am experiencing is the printing of the relative path for each referenced node. I have tried the following twig commands but I have not had any success so far:






The following allows me to manually specify the node's id in twig, but that's not what I'm looking for:


Ideas?

Notes / Thoughts: In my first code snippet, the fields in & # 39; top_region & # 39; contain only the title of the nodes. There was an option to link the field to the content but I had to add the & # 39;| striptag& # 39; twig filter due to extra markup disturb style. Is it possible to retrieve the path of the URL from the title of the node?

theming – When I'm going to disable the Twig template cache, fatal error

When I disable the Twig template cache, fatal error. it is to say

The website has encountered an unexpected error. Please try again later.
Error: Operand types not supported in
Drupal Core Template TwigEnvironment -> __ construct () (line 69 of
core lib Drupal Core Template TwigEnvironment.php).

I followed the steps below:

1. Copy and rename the file from sites / example.settings.local.php to sites / default / settings.local.php.
2. Open the settings.php file in sites / default and uncomment the following lines:

if (file_exists ($ app_root. & # 39; / $.; site_path. & # 39; /settings.local.php')) {
include $ app_root. & # 39; / & # 39; $ site_path. & # 39; /settings.local.php&#39 ;;
}

3. Do not comment these lines in settings.local.php

$ parameters['cache']['bins']['render']    => Cache.backend.null & # 39 ;;
$ parameters['cache']['bins']['dynamic_page_cache']    => Cache.backend.null & # 39 ;;
  1. Open development.services.yml in the Sites folder and add the following code

    settings:
    twig.config:
    debugging: true
    auto_reload: true
    cache: false

  2. Rebuild the cache and good to go

theming – places the fields of a view in twig from a custom view style

I have therefore created a custom display style as a result of a tutorial and enabled the use of fields. However, I can not understand what twig file variables should be to display the fields I want.

My PHP looks like this

class Slider expands StylePluginBase {



/ **
* {@inheritdoc}
* /
protected function defineOptions () {
$ options = parent :: defineOptions ();
$ options['path'] = array (& # 39; default & # 39; => & # 39; slider & # 39;);
returns $ options;
}

/ **



* Does the style plugin itself support the addition of fields to its output?
*
* @var bool
* /
protected $ usesFields = TRUE;



/ **
* {@inheritdoc}
* /
public function buildOptionsForm (& $ form, FormStateInterface $ form_state) {
parent :: buildOptionsForm ($ form, $ form_state);

// Path prefix for SLIDER links.

$ form['chooseImage'] = [
      '#title' => $this->t('Choose Image'),
      '#type' => 'select',
      '#description' => t('Choose where to position the title and text in the slider'),
      '#options' => ['cc' => $this->t('Center'), 'tl' => $this->t('Top Left')],
];

}

I create a theme function for the view.

function slider_theme ($ existing, $ type, $ theme, $ path) {
return a table (
& # 39; slider & # 39; => array (
& # 39; line & # 39; => & # 39; extended_display_styles.theme.inc & # 39;

)
)
}

The theme.inc file of extended display styles allows me to add options, but I do not know how to add view variables.

    function template_preprocess_views_view_slider (& $ variables) {
// Display options set by the user.


$ options = $ variables['view']-> style_plugin-> options;

// Build a two-dimensional table with years and months.
$ time_pool = array ();
$ frameArrayManual = array (& # 39;
// $ options['manual'] = $ variables['view']-> style_plugin-> manual;

foreach ($ variables['view']-> result as $ id => $ result) {
$ created = $ result-> node_field_data_created;
$ created_year = date (Y & # 39 ;, created);
// Date format of the month.
$ month_date_format = (isset ($ options['month_date_format']))? $ options['month_date_format'] : & Mr;
$ created_month_digits = date (& # 39; m, $ created);
$ created_month = date ($ month_date_format, $ created);
$ time_pool[$created_year][$created_month_digits]    = "$ created_month";
}

$ options['time_pool'] = $ time_pool;
$ options['frameArrayManual'] = array (& # 39 ;, & quot; b & # 39 ;, & quot;) & quot;

// Update options for twig.
$ variables['options'] = $ options;
}

I do not find any resources to understand that.

Hide parent terms in the view block of taxonomy terms in Twig

Try to hide the first two terms (or the first two levels) of my SEO by twig. The list itself is a view block using the term reference tree formatter.

enter the description of the image here

8 – Rewrite the image output in the views: how to provide a replacement pattern in a twig tag?

In the views, I want to rewrite the output of the image field using the twig function drupal_image (see Twig Tweak module). When I directly provide the path of the image, this code works:

{{drupal_image (public: //myimage.jpg&#39 ;,response_style_image, responsive = true)}}

But I do not manage to provide the path of the image by a variable, like this pseudocode:

{{drupal_image (& # 39; {{uri}}, responsive_style_image, responsive = true)}}

The problem seems to be how to provide the variable {{}} Uri properly inside the surrounding twig label.

8 – Twig and CSS for custom block do not work

I'm working on a custom module that has a block. This block has a form in configuration in which the user enters the relative data; so far, it saves data and displays it, but without a template (twig). I'm trying to display it through a twig but that's not possible.

I read a lot of documentation but I did not manage to make it work. Not really sure what the problem is or what I miss

That's the structure of my module

module
------Customs
------------ onyx_experiencia
---------------------------- onyx_experiencia.info.yml
---------------------------- onyx_experiencia.libraries.yml
---------------------------- onyx_experiencia.module
---------------------------- css
------------------------------- onyx_serv_css.css
---------------------------- models
------------------------------------- onyx-experiencia.html.twig
---------------------------- src
-------------------------------Connect
-------------------------------------Block
------------------------------------------ onyx_experiencia.php

onyx_experiencia.info.yml

Name: Onyx Servicios
Description: Block of Servicios de OnyxGroup
Core: 8.x
package: customized
dependencies:
- block
type: module

onyx_experiencia.libraries.yml

onyx-experiencecia-style:
version: 1.x
css:
theme:
css / onyx_serv_css.css: {}

onyx_experiencia.module

    [
& # 39; variables & # 39; =>[
& # 39; CardTitle & # 39; => NULL,
],
],
];
}

onyx_experiencia.php

getConfiguration ();
$ build = array ();

if (! empty ($ config['card_title'])) {
$ name = $ config['card_title'];
}
other {
$ name = $ this-> t (Onyx Group Map & # 39;);
}

if (! empty ($ config['card_text'])) {
$ cardText = $ config['card_text'];
}
other {
$ cardText = $ this-> t (& # 39; One Onyx Group Service & # 39;);
}

// $ build[]['#attached']['library'][]    = & # 39; onyx_experiencia / onyx_serv_css & # 39 ;;
$ build = array (
& # 39; # theme & # 39; => & # 39; onyx-experiencia & # 39 ;,
)

to build['CardTitle'] = table (
& # 39; # prefix & # 39; => & # 39;
& # 39; # markup & # 39; => $ name,
& # 39; # suffix & # 39; => & # 39;
)

to build['CardText'] = table (
& # 39; # prefix & # 39; => & # 39;
& # 39; # markup & # 39; => $ cardText,
& # 39; # suffix & # 39; => & # 39;
)

$ cardImage = $ this-> configuration['card_image'];
if (! empty ($ cardImage[0])) {
if ($ file = File :: load ($ cardImage[0])) {
to build['CardImg'] = table (
& # 39; # theme & # 39; => & # 39; image_style & # 39 ;,
& # 39; # style_name & # 39; => & # 39 ;,
& # 39; # uri & # 39; => $ file-> getFileUri (),
)
}
}

$ cardLink = $ this-> configuration['card_link'];
to build['CardLink'] = table (
& # 39; # markup & # 39; => $ cardLink,
)

return $ build;

}

/ **
* {@inheritdoc}
* /
public function blockForm ($ form, FormStateInterface $ form_state) {
$ form = parent :: blockForm ($ form, $ form_state);

$ config = $ this-> getConfiguration ();

$ form['card_title'] =[
& # 39; # type & # 39; => & # 39; textfield & # 39 ;,
& # 39; # title & # 39; => $ this-> t ('Card title'),
& # 39; # description & # 39; => $ this-> t (& # 39; Type the title of the service card. & nbsp;)
& # 39; # default_value & # 39; => isset ($ config['card_title'])? $ config['card_title'] : & # 39;
];

$ form['card_text'] =[
& # 39; # type & # 39; => & # 39; textarea & # 39 ;,
& # 39; # title & # 39; => $ this-> t ('Map Text'),
& # 39; # size & # 39; => & # 39; full_html & # 39;
& # 39; # description & # 39; => $ this-> t (& # 39; Type list of services. & # 39;),
& # 39; # default_value & # 39; => isset ($ config['card_text'])? $ config['card_text'] : & # 39;
];

$ form['card_image'] =[
& # 39; # type & # 39; => & # 39; managed_file & # 39;
& # 39; # title & # 39; => t ('Background of the map image'),
& # 39; # upload_validators & # 39; => array (
& # 39; file_validate_extensions & # 39; => array (& gt; gif png jpg jpeg & # 39;),
& # 39; file_validate_size & # 39; => table (25600000),
)
& # 39; # theme & # 39; => & # 39; image_widget & # 39;
& # 39; # preview_imgage_style & # 39; => & # 39 ;,
& # 39; # upload_location & # 39; => & # 39; private: // card_service_img & # 39;
& # 39; # progress_message & # 39; => For a moment while we save your file ...
& # 39; # default_value & # 39; => isset ($ this-> configuration['card_image'])? $ this-> configuration['card_image'] : & # 39;
& # 39; # required & # 39; => TRUE,
];

$ form['card_link'] =[
& # 39; # title & # 39; => t (& # 39; Type of map link. Example: / erp_cloud & # 39;),
& # 39; # type & # 39; => & # 39; url & # 39;
& # 39; # default_value & # 39; => isset ($ this-> configuration['card_link'])? $ this-> configuration['card_link'] : & # 39;

];

return $ form;
}

/ **
* Custom submission actions
* /
public function custom_submit_form ($ form, FormStateInterface $ form_state) {
$ values ​​= $ form_state-> getValues ​​();
// Perform the required actions
}

/ **
* {@inheritdoc}
* /
Public function blockSubmit ($ form, FormStateInterface $ form_state) {

$ cardImage = $ form_state-> getValue (& # 39; card_image & # 39;);
if ($ cardImage! = $ this-> configuration['card_image']) {
if (! empty ($ cardImage[0])) {
$ file = File :: load ($ cardImage[0])
$ file-> setPermanent ();
$ file-> save;
}
}

$ this-> configuration['card_title'] = $ form_state-> getValue (& # 39; card_title & # 39;);
$ this-> configuration['card_text'] = $ state_format-> getValue (& # 39; card_text & # 39;);
$ this-> configuration['card_image'] = $ state_format-> getValue (& # 39; card_view & # 39;);
$ this-> configuration['card_link'] = $ state_format-> getValue (& # 39; map_link & # 39;);
}

}

onyx_experiencia.html.twig

{#
/ **
* @file
* Profile of the onyx_experiencia block.
* /
#}
{{attach_library (& # 39; onyx_experiencia / onyx_serv_css & # 39;)}}

{{}} Form.CardTitle