8 – Trigger a Drupal behavior from another module in Javascript?

The block_ajax module defines a behavior in its module:

https://git.drupalcode.org/project/block_ajax/-/blob/8.x-1.x/js/refresh_ajax_block_command.js

I want to initiate this call every 60 seconds so the contents of the block can refresh if there is new data from my own script file.

I tried two ways.

The first way I tried was setting up a route for a simple controller that returned an AjaxResponse:

  public function execute() {
    $response = new AjaxResponse();
    $response->addCommand(new RefreshBlockAjaxCommand('.my_block'));
    return $response;
  }

Hitting the route with Drupal.ajax results in ‘context.querySelectorAll is not a function’, but it appears that the block is replaced (but missing the block wrapper around it, oddly enough, which probably won’t work calling it over and over).

The second way is I tried to call it from the browser console with Javascript, but I can’t figure out between this answer and the linked Javascript file at the top of the question what to call.

theming – Connecting Views – Drupal Answers

Novice here. I’m on Drupal 7.

I have a content type with a title, image and body field. I’ve created two views to display that content. The two views are on a page and I need help connecting them so that clicking/tapping on the image in View 2 will change content in View 1. Ideally this happens without page reload and View 1 fades in as new image in View 2 is clicked/tapped.

This seems like a filtering thing but I can’t filter using images, I don’t think!? Are Views the best way to set this up? If yes, what specifically needs to happen for the two Views to connect? If there is an easier way to set this up, what would it be? Help all! Any direction is appreciated.

Views

Drupal 8, Views: Count how many children have a reference to the parent

Explanations:

1) I have a node “parent”

2) I have 2 nodes “child”

3) Each child has a reference field (filled with a reference to the node “parent”)

Now, I’m trying to create a list with the Views module.

First problem: This list is a list of “parent” nodes AND should include a column with the following result: COUNT(child with a reference to the parent).

Second problem: The count result should include only published children.

Example 1 with 2 children published (2 children have a reference to the parent):

Title of parent node | 2

Example 2 with 1 child published (2 children have a reference to the parent):

Title of parent node | 1

Sorry if it’s not totally clear, it’s hard to explain

Above all of this, if the result could be sortable it will be a good news but I don’t think it will be possible…

Thank you

8 – Is there any native Drupal way to implement AJAX form submission without a button?

I need to implement a form that automatically saves data on each element change. So I can add a button with ajax callback and trigger mousedown on this button on “change” event using jQuery.
And naturally I should hide this button. So this works ok, but it seems to be some ugly solution.

Is there any native Drupal way to make ajax form submission on each element change without a hidden button?

8 – How to get drupal to check for a twig template in the active theme folder before checking the module folder?

I’m developing a contrib module for Drupal 8 that sends email notifications to group members when new content has been posted in groups they have subscribed to. The module defines some new view modes for nodes and comments. It then uses these new view modes to render the contents of the emails it sends out. The module defines a theme hook suggestion for each view mode and supplies a twig template for each view mode.

It works fine as is. The twig templates from within the module are utilized without a problem. What I’m not understanding is why copies of these templates placed within the active theme are not utilized. I’m familiar with the process of copying an existing twig template into a theme to be modified. But no matter what I do the template from the module is always used in this case. It was my understanding Drupal searches for suitable twig templates in the theme first and then works backward but perhaps my module is missing something.

Here are code samples for more details.

Define the view mode for comments – in modules/contrib/group_subscription/config/install/core.entity_view_mode.comment.group_subscription_immediate

langcode: en
status: true
dependencies:
  module:
    - comment
id: comment.group_subscription_immediate
label: 'Group Subscription Immediate (comment)'
targetEntityType: comment
cache: true

Define the theme hook suggestion – in modules/contrib/group_subscription/group_subscription.module

/**
 * Implements hook_theme()
 */
function group_subscription_theme($existing, $type, $theme, $path) {
  return (
    'comment__group_subscription_immediate' => (
      'base hook' => 'comment',
    )
  );
}

/**
 * Implements hook_theme_suggestions_comment_alter()
 */
function group_subscription_theme_suggestions_comment_alter(array &$suggestions, array $variables) {
  if ($variables('elements')('#view_mode') == 'group_subscription_immediate') {
    $suggestions() = 'comment__group_subscription_immediate';
  }
}

Please let me know if there is anything else I can provide to help me figure this out. I’ve been working with Drupal for years but am still relatively new to D8 and this is my first attempt at creating a module to give back. Thanks in advance!

How to add class to all text inputs in Drupal 8?

Explain please how to add same class (form__text) to all text, email, password, search, textarea inputs in Drupal 8?

How to get list of classes implementing particular interface in Drupal 8?

I need to have a list of classes implementing Drupal 8 User Auth Interface & be able to reference a particular implementation class (such as Facebook Auth). How do I do this please? I don’t want to directly modify the core Drupal 8 User Auth class which is currently the only class implementing the User Auth Interface.

views – PHP error in the log after PHP and Drupal upgrade

I got this error message in the log but not sure what is it about and how to fix it. Has anyone seen the same issue before and know how to fix it? Thank you so much!

DrupalCoreEntityEntityMalformedException: The “view” entity cannot have a URI as it does not have an ID in DrupalCoreEntityEntityBase->toUrl() (line 192 of /var/www/drupal_dev_8.8.6/core/lib/Drupal/Core/Entity/EntityBase.php)

Deploy Drupal 8 in AWS

I want to deploy a drupal 8 system from localhost to aws , aws provide diferents services , what of this services is the best for my app? , the system is a little managment system , it will be used for 3 or 4 users to create content basically.

Custom tokens breaking on upgrade to Drupal 9

I’m getting a crash on node save when my node contains a custom token. I’m using the Token Filter module to display tokens in node content.

To make the tokens I followed this tutorial, which is similar to the Token documentation.

Now, with Drupal 8.8, when I save a new node that contains one of my custom tokens, I get a WSOD with this error:

DrupalCoreEntityEntityStorageException: The “node” entity cannot
have a URI as it does not have an ID in
DrupalCoreEntitySqlSqlContentEntityStorage->save() (line 846 of
core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

But the real error appears to be from token:

DrupalCoreUtilityToken->generate('ebook', Array, Array, Array, Object) (Line: 196)
DrupalCoreUtilityToken->replace('
Test (ebook:custom-type) 2

', Array, Array, Object) (Line: 130)
Drupaltoken_filterPluginFilterTokenFilter->process('
Test (ebook:custom-type) 2

', 'ja') (Line: 118)
DrupalfilterElementProcessedText::preRenderText(Array)
call_user_func_array(Array, Array) (Line: 100)
DrupalCoreRenderRenderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements DrupalCoreSecurityTrustedCallbackInterface or be an anonymous function. The callback was %s. Support for this callback implementation is deprecated in 8.8.0 and will be removed in Drupal 9.0.0. See https://www.drupal.org/node/2966725', 'silenced_deprecation', 'DrupalCoreRenderElementRenderCallbackInterface') (Line: 781)
DrupalCoreRenderRenderer->doCallback('#pre_render', Array, Array) (Line: 372)

Weirdly, if I remove the token from the body field when I save the node, the node saves properly– and then I can edit the node and add the token back in without any error. The error only occurs when the node is saved and it has a token in it.

Drupal tokens like (site:name) work fine; it’s just my custom token that causes an error.

Here’s the relevant code:

/**
 * Implements hook_token_info().
 */
function MYMODULE_token_info() {

  $ebook_type = (
    'name' => t('Custom Tokens'),
    'description' => t('Tokens to make upgrading to D9 harder.'),
  );

  $ebook('custom-type') = (
    'name' => t("Custom type"),
    'description' => t('Apples or oranges'),
  );

  return (
    'types' => ('ebook' => $ebook_type),
    'tokens' => ('ebook' => $ebook),
  );

}

/**
 * Implements hook_tokens().
 */
function MYMODULE_tokens($type, $tokens, array $data, array $options, DrupalCoreRenderBubbleableMetadata $bubbleable_metadata) {

  $replacements = ();

  if ($type == 'ebook' && !empty($data('node'))) {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'custom-type':
          $replacements($original) = MYMODULE_token_set_custom_type($data('node'));
          break;
      }
    }
  return $replacements;
}

function MYMODULE_token_set_custom_type($node) {
  $alias = $node->toURL()->toString();
  preg_match('(/.*/)', $alias, $matches);
  if ($matches(0) == '/apples/') {
    return "Apples";
  }
  else {
    return "Oranges";
  }
}

How do I resolve this?