Account created on 14 May 2022, over 2 years ago
#

Recent comments

I provided you with the sample code, it needs to be modified to fit your own needs. 

May need to try this. 

/**
 * Implements hook_preprocess_HOOK() for taxonomy term templates.
 */
function mytheme_preprocess_taxonomy_term(&$variables) {
  // Only act on the 'etats' vocabulary.
  if ($variables['vocabulary_machine_name'] == 'etats') {
    $term = $variables['term'];

    // Load the nodes referencing this term.
    $nids = \Drupal::entityQuery('node')
      ->condition('field_etat', $term->id())
      ->execute();

    if (!empty($nids)) {
      $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
      // Render the nodes using the 'teaser' view mode.
      $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
      $variables['referencing_nodes'] = $view_builder->viewMultiple($nodes, 'teaser');
    }
  }
}

And then 

{# taxonomy-term--etats.html.twig #}
<div{{ attributes }}>

  {# Render the field_map field #}
  {{ content.field_map }}

  {# Render the referencing nodes in the second position #}
  {% if referencing_nodes is not empty %}
    <div class="referencing-nodes">
      {{ referencing_nodes }}
    </div>
  {% endif %}

  {# Render the description field #}
  {{ content.description }}

</div>

Or 

Second solution: Modify the twig only 

{# Render the 'field_map' first #}
{{ content.field_map }}

{# Load and render referencing nodes in teaser view mode #}
{% if term %}
  {% set referencing_nodes = drupal_view('view_machine_name', 'display_id', term.id) %}
  {{ referencing_nodes }}
{% endif %}

{# Finally, render the 'description' field #}
{{ content.description }}

And then create or modify a view to fetch referencing nodes

Create a new view

  • Show Content of type that references the taxonomy term through a field (like your field_etat in content types).
  • Add a Contextual Filter for the field that references the taxonomy terms.
  • Choose the Teaser display mode for these nodes.
  • Ensure the machine name and display ID match those used in your Twig file (view_machine_name, display_id).

You need to trace the source of warning 

\Drupal::logger('module_name')->notice('<pre>' . print_r($render_array, TRUE) . '</pre>');

 

Re-git and run "composer update" again. 

1. Make sure the field exists or not

  • Go to Manage → Structure → Field storage (the path may vary slightly depending on your Drupal version and setup) and check if field_normalstring is listed under the user fields.
  • If you're working in a development environment where fields are managed through code (like with Features or config sync), making sure the field storage definitions are correctly defined in your configuration files.

2. If the field does not exist, you'll have to create it. This can be done through the UI or using a Drupal module with an update hook or default configuration.

use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;

/**
 * Implements hook_update_N().
 */
function your_module_update_8001() {
  // Check if the field storage does not exist.
  if (!FieldStorageConfig::loadByName('user', 'field_normalstring')) {
    // Create field storage.
    FieldStorageConfig::create([
      'field_name' => 'field_normalstring',
      'entity_type' => 'user',
      'type' => 'string',
    ])->save();

    // Create the field instance on the user entity.
    FieldConfig::create([
      'field_name' => 'field_normalstring',
      'entity_type' => 'user',
      'bundle' => 'user',
      'label' => 'Normal String',
      // Set other settings as necessary.
    ])->save();
  }
}

3. Clear Cache

I think you need to produce a hook. 

function your_theme_preprocess_taxonomy_term(&$variables) {
  if ($variables['term']->bundle() === 'etats') {
    // Check if the nodes referencing this term are loaded and available in the content render array.
    if (isset($variables['content']['your_nodes_reference_field'])) {
      // Temporarily store the nodes list.
      $nodes_list = $variables['content']['your_nodes_reference_field'];

      // Remove it from its original place.
      unset($variables['content']['your_nodes_reference_field']);

      // Create a new order of elements as per your preference.
      $new_content_order = [
        'field_map' => $variables['content']['field_map'],
        'your_nodes_reference_field' => $nodes_list, // Position the nodes list second.
        'description' => $variables['content']['description']
      ];

      // Overwrite the original content array with the new order.
      $variables['content'] = $new_content_order;
    }
  }
}

And then you need to do is to modify your twig: 

{{ content.field_map }}
{{ content.your_nodes_reference_field }}  {# Ensure this is the correct machine name #}
{{ content.description }}

Here is the following: 

Create a customised module: 

Here's an example of what the code might look like:
 

<?php

namespace Drupal\your_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Provides a block to display 'Node Bundle' of the current node.
 *
 * @Block(
 *   id = "node_bundle_block",
 *   admin_label = @Translation("Node Bundle Block")
 * )
 */
class NodeBundleBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs a new NodeBundleBlock.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('current_route_match')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $node = $this->routeMatch->getParameter('node');

    // Check if the current route has a node object and retrieve the bundle.
    if ($node && $node instanceof \Drupal\node\NodeInterface) {
      $bundle = $node->bundle();  // This gets the node type (bundle)
      return [
        '#markup' => $this->t('The node bundle type is: @type', ['@type' => $bundle]),
      ];
    }

    return [
      '#markup' => $this->t('No node available.'),
    ];
  }
}

Using a Theme Template (e.g., block--yourblock.html.twig)

Implement hook_preprocess_block() in your theme's .theme file:

function your_theme_preprocess_block(&$variables) { 
// Check if the block id matches your block 
if ($variables['plugin_id'] == 'your_block_plugin_id') { 
$node = \Drupal::routeMatch()->getParameter('node'); 
if ($node instanceof \Drupal\node\NodeInterface) { 
$variables['node_bundle'] = $node->bundle(); 
} 
} 
}

Modify the Twig Template:

In your block--yourblock.html.twig, you can now use:

{% if node_bundle %}
  <p>The node bundle type is: {{ node_bundle }}</p>
{% else %}
  <p>No node available.</p>
{% endif %}

Thank you Damien, but I think once it has the path alias also it appears in the Link field, it should save as path alias with canonical active.

The linkit-n2877535-50.patch is successfully changed the URL Link field from "node/xxx" to URL Alias, but it still needs to work, as the saved URL is still "node/xxx" rather than saved as its path alias, 'canonical' is not active.

Production build 0.71.5 2024