New answers tagged

0

isAnonymous() holds the danger of not working when a site is saving data (for example in forms) in PrivateTempStore. Therefore, i prefer the following: if (\Drupal::currentUser()->isAuthenticated()) { // This user is logged in. } else { // This user is anonymous. }


0

If you don't need to update the link tag, then just use the download attribute, it should do the trick. I've wrote a tiny Fiddle for you https://jsfiddle.net/45ug9p7t/1/ Keep in mind that not every browser has implemented this, check it out


0

As @Hudri said, you can use MYMODULE_form_views_exposed_form_alter() to alter the form. Rather than check each term to see if it's parent is your contextual filter argument, another approach might be to use the TermStorage::loadTree() method to load the tree of terms below a term, e.g. $vocabulary_id = 'tags'; // Get this from your view object's ...


0

Right, I found a solution myself. There are 2 options; Views Create a new view with the user entity, set a filter criteria for logged in user and provide a rest export. This will return the current user object. So you can make a request to this when login returns a 403 so you can retrieve the details of the user. Logout token Apply this patch to the user ...


3

Can somebody tell my, what these attacks are about? They're requests from bots fishing for vulnerabilities in your website. Was there a bug in Drupal making it vulnerable for those attacks? Vulnerable to the specific attempts you've listed? Looking at the exact nature of the URLs, probably not. But there was, of course, Drupalgeddon once upon a time, so ...


-1

https://www.drupal.org/project/download_file You can try this above module and check.


0

I suspect you are installing Drupal (or at least trying to) on a server running PHP 7.4, which is not currently compatible with the latest version of Drupal (8.8). There is an issue on drupal.org right here that details the current problems, including yours: https://www.drupal.org/project/drupal/issues/3086374 . The highest supported version of Drupal 8.8 ...


1

I've done this recently in a project of mine using MYMODULE_form_views_exposed_form_alter. The code assumes you've a view with the term ID on first position as contextual filter, e.g. domain.com/myview/123 <?php function MYMODULE_form_views_exposed_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { $view = $form_state-&...


1

There is a patch here in progress to allow wild cards in breadcrumbs: https://www.drupal.org/project/easy_breadcrumb/issues/3051106


0

Entity Type and bundle name are two things; usually you want to have both. You might e.g. have a media entity of the bundle "image". Bundle names alone may not be unique, I think you could name a content type "gallery" and also a paragraph type "gallery" without getting a validation error. So: $entity->getEntityTypeId() gives you the entity type: node, ...


1

You should be able to get that from ModerationInformation:: shouldModerateEntitiesOfBundle(), via the content_moderation.moderation_information service: $moderation_info = \Drupal::service('content_moderation.moderation_information'); $entity_type = \Drupal::entityTypeManager()->getDefinition('node'); if ($moderation_info->...


0

Views are config entities so you can target them to be referenced (see screenshots) and check this post. The caveat is you can only point to a given entity type when using ER fields so you cannot have an ER field targeting content and config alike. On the other hand almost any market solution referencing views aim to render it not just displaying a link ...


0

Warning: this may have security implications, make fully sure the $criticalCss variable contains only css you trust. Just leaving a useful snippet related to inline CSS that needs to be added in <head>; it can be done like this: use Drupal\Core\Render\Markup; function mymodule_preprocess_html(&$variables) { // **WARNING**: make sure critical....


0

You can't change elements on callback or validateForm(), you need to put it on buildForm() method. Here is a full example: /method buildForm $result = \Drupal\my_module\Controller\Contaminante::list(); //database logic foreach ($result as $it){ $arrSelectContaminantes[$it["id_contaminante"]] = t($it["descripcion"]); } $form['...


-1

{% set body_classes = [ logged_in ? 'user-logged-in', not root_path ? 'path-frontpage' : 'path-' ~ root_path|clean_class, node_type ? 'page-node-type-' ~ node_type|clean_class, db_offline ? 'db-offline', user.getRoles()|join(' role-'), 'edit' in url('<current>')|render|render ? 'page-edit' ] %}


0

I had same issue and I have done this way: first create view page with alias, for example "alias-test" then go to admin/config/search/path and click "add alias" choose language you want to translate, in my case I choose "PT" on "existing system path" write your path "alias-test" and in "path alias" added alias translation "teste-alias" This works with ...


0

I finally understood that keys in source is the way to make each row of the csv file unique. It may be anything. So I changed to: keys: - contract_id - member_id I removed id: idfrom processand it works! The value of id field in the database is properly auto-incremented.


-2

You can try code rsvplist.form: path: '/rsvplist/{nid}' .... use Drupal\node\NodeInterface; .... $nid= \Drupal::routeMatch()->getParameter('nid'); $node = Node::load($nid);


0

Thanks to berramou! I ended up using this in my theme: /** * Implements hook_preprocess_HOOK(). */ function MYTHEME_preprocess_node(array &$variables) { if ($variables['view_mode'] == 'search_result') { $variables['#cache']['max-age'] = 0; } }


2

Your route doesn't have a parameter called node, so therefore $node is null. In order to make node available as a parameter you have to be on a route that has node as a parameter. So to add the node parameter to your route, you can do like this: rsvplist.form: path: '/rsvplist/{node}' defaults: _form: '\Drupal\rsvplist\Form\RSVPForm' _title: '...


0

There is another possibility as pointed in the documentation You could use drush and run the following command: drush php-eval "echo drupal_get_installed_schema_version('module_name');" to find the last applied update hook for a specific module.


0

As a proof of concept a module with this functionality already exists. Unfortunately, it's not published yet because that requires a couple of final touches with a time frame allocated for this kind of work. https://github.com/drugan/commerce_oiaddon The price adjustment is taken from a pseudo-price field added to some entity type and then the order item ...


0

I got a similar error on another (custom) views page and it helped me understand the cause of this error. This time, the full error was: Error: Call to a member function getId() on null in Drupal\Core\Entity\EntityViewBuilder->getBuildDefaults() (line 223 of C:\htdocs\myproject\www\core\lib\Drupal\Core\Entity\EntityViewBuilder.php) #0 C:\htdocs\myproject\...


1

You can make use of Dynamic Queries instead of Database::getConnection()->query('SELECT expires_at FROM {sessions} s WHERE s.uid = :uid', [':uid' => $uid]); You can do: // Create an object of type Select. $database = \Drupal::database(); $query = $database->select('sessions', 's'); // Add extra detail to this query object: a condition, ...


1

You are almost there just you should change $variables['element'][$tid]['#attributes']['class'] instead of $value['#attributes']['class'] try with: /** * Implements hook_preprocess_HOOK(). */ function template_preprocess_bef_radios(&$variables) { // No need to this two lines // $variables['children'] = Element::children($variables['element']); // ...


1

I could not quite get my mocked service to be used from the container as per the docs, so I resulted to doing this. I don't really need a BrowserTest per se, as I just want to ensure the Response object contains what it should. I create a client mock and instantiate the controller object myself: <?php namespace Drupal\Tests\mymodule\Kernel; use Drupal\...


4

It seems like you might want to look into the Password Policy module which probably has a lot of the features you are trying to rebuild. I also wonder whether it makes sense to build your own user inscription form instead of altering the existing core registration form. Altering forms tends to result in easier integrations with existing modules.


0

You can use hook_ENTITY_TYPE_insert which is called after the node has been saved. Function description: This hook runs once the entity has been stored. Note that hook implementations may not alter the stored entity data.


0

You should not really rm anything it is not required. composer remove drupal/coder composer require --dev drupal/coder Also for a bonus point it's often a good idea to include the hash in the composer file incase an unstable dev is released you won't have to worry about changes that have come after.


0

Thanks for the comments, both of you have actually redirected me in the right direction. Below are the actual steps that I took to achieve this: Create a link field (field_url) in one of the content types using admin UI. and use the below code to use that field widget in your custom form or custom block. $form['#parents'] = []; // Create an empty ...


1

I found the solution. I just copied the media.html.twig template from classy theme to my custom theme which did not work. We need to copy this file also container.html.twig to our custom theme's templates directory then it will work.


0

Core's link field actually uses a '#type' => 'url' form element as base and if the field is configured to support internal links it switches to '#type' => 'entity_autocomplete'. See https://git.drupalcode.org/project/drupal/blob/8.8.0/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php#L186-203 So if you just want to be able to select ...


0

You can configure thoses fields by doing some backend work, as you said you are using dropzone, please have a at their webpage dropzonejs where you can find a documentation and also it gives you link to use it up to your tech! Here an example of I use it in a form : $form['photo_principale'] = [ '#type' => 'dropzonejs', '#title' => $this->t('...


-2

If you have a link field attached to any entity like a node bundle, or user or ... then you can use a code like this : //Create an empty representative entity $node = \Drupal::service('entity_type.manager')->getStorage('node')->create(array( 'type' => 'article' ) ); //Get the EntityFormDisplay (i.e. the default Form Display) ...


1

I think you have to create two seperate form fields to do this in Drupal 8. You can use textfield for the title and url for the URL field, like this: $form['cta_title'] = [ '#type' => 'textfield', '#title' => t('CTA Title'), ]; $form['cta_url'] = [ '#type' => 'url', '#title' => t('CTA Url'), ]; return $form; Another solution is to ...


1

In code you can only override globally, but each instance of the entity class would be for one specific bundle, this is the only required field when you create a content entity with bundles, so the bundle is always set and you can add a condition to the overridden method. For example if you had a getTitle() method you could modify the title for a specific ...


0

The following snippet works for me on Drupal 8.7 drush ev '\Drupal::service("router.builder")->rebuild();'


-1

well if you are making a custom block then you are probably doing something like this .... $build = []; $build['#theme'] = 'your_template', return $build; and then in you hook_theme(). hook_theme() { return [ 'your_template' => [ 'render element' => 'children', 'template' = 'your-templante-name', ], ]; } you don't need to ...


3

Hello try this ... $form['field_prestation_walk']['#states'] = [ 'visible' => [ ':input[name="field_prestation_type"]' => [ 'value' => 'walk' ], ] ];


0

Something that was, at one time, fairly simple to figure out and find documentation for has become quite a bit more confusing and hard to find. This is one of the top search results for this topic, so I want to take the time to post a solution I was able to put together using the new Methods. My use case is getting a list of published nodes of a certain ...


0

You can use the hook_form_FORMID_alter in order to add classes to the fields you want to. /** * Implements hook_form_FORM_ID_alter(); */ function hook_form_FORM_ID_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { $form['my_custom_field']['#attributes']['classe'][] = "my-custom-class" }


2

You can create a controller in its simplest form. In my_module.routing.yml : my_module.profile_controller_profile: path: '/user/{user}/my-profile' defaults: _controller: '\Drupal\my_module\Controller\ProfileController::profile' _title: 'Profile' requirements: _permission: 'edit profile' user: \d+ In ProfileController.php : <?php ...


0

Try hiding it using css: .field--name-revision-log-message { display: none; } I used this for media.


0

That's simple. In your form's build() function check for an existing entity for that user. If there is one, prefill the fields (set #default_value). Maybe add the existing entity-id as hidden field (if any). In your form's submit() check again for an existing entity (for security reasons) or use the hidden entity-id. Then decide in your code if you create ...


3

lando db-import <path to sql or sql.gz or .zip file here> will do that for you. db-import will drop all tables before performing the import. it has a --no-wipe flag that can be used it you don't want to drop tables first. More information in the documentation: https://docs.lando.dev/guides/db-import.html


0

We need to run the following two commands to import database (to an existing site) lando drush sql-drop lando db-import <dbname>.gz


0

try this code: {% if links -%} {%- if heading -%} {%- if heading.level -%} <{{ heading.level }}{{ heading.attributes }}>{{ heading.text }}</{{ heading.level }}> {%- else -%} <h2{{ heading.attributes }}>{{ heading.text }}</h2> {%- endif -%} {%- endif -%} <ul{{ attributes }}> {%- for item in links -%} <li{{ ...


0

The content id is missing from your function call you have to add it like this {{ drupal_view('who_s_new', 'block_1', arg_1, arg_2, arg_3) }} or you can render your view in custom block and add it in every page via 'block layout' https://www.webwash.net/programmatically-create-block-drupal-8/ $view = views_get_view('view_name'); $view->set_display('...


1

Issue #2839833 has a proposal to add entity clone plugin to node entity to handle this special case. Alternatively, we could use the event. Luckily Entity Clone module dispatches an event before & after clone for the other modules to act. Here is the code for menu copy: // mymodule.services.yml mymodule.entity_clone.subscriber: class: Drupal\...


0

Some forms generated by Drupal 8(.7.8)'s FAPI crash when cached server side using Drupal 8's cache, and loaded by an anonymous user. If you clear the server side cache($ drush cr), the anonymous user can then load the form successfully; on refresh it crashes once cached server side and reloaded by the user. In this case, BrianV's answer ($form['#cache'] = [...


Top 50 recent answers are included