Graph in custom text Drupal field

I tried many times to insert a graph using CSS/HTML/TWIG in a Drupal View field. The problem is that when I put the code in the text field area, style="width: {{ actual_time_needed }}%;", it doesn’t appear. Where’s the problem, and what is the best approach to resolve it?

<div class="graph-container">
  <div class="graph-bar" style="width: {{ actual_time_needed }}%;" title="Actual Time Needed: {{ actual_time_needed }} hours;">
    Actual Time Needed ({{ actual_time_needed }} hours)
  </div>
  <div class="graph-bar" style="width: {{ tech_senior_time }}%;" title="Tech Senior Developer: {{ tech_senior_time }} hours;">
    Tech Senior Developer ({{ tech_senior_time }} hours)
  </div>
</div>
.graph-container {
  width: 100%; /* Full width */
  background-color: #f3f3f3; /* Background color of the container */
  border-radius: 5px; /* Rounded corners */
  overflow: hidden; /* Prevent overflow */
  margin-bottom: 20px; /* Space below the graph */
}

.graph-bar {
  height: 30px; /* Height of each bar */
  background-color: #4CAF50; /* Color for the bars */
  text-align: center; /* Center the text */
  line-height: 30px; /* Center the text vertically */
  color: white; /* Text color */
  font-weight: bold; /* Make text bold */
  transition: width 0.3s ease; /* Smooth transition for width change */
}

I checked in the dev tools and on the website.

  1. Check if Twig is Enabled in the View

Make sure that Twig rendering is enabled for the field where you’re inserting your code. In Drupal, the “Text” field type often does not allow Twig rendering by default. Instead, you might need to use a “Custom template” or a “Field Template” to ensure that your Twig code is processed.
2. Use Custom Template for the View Field

Instead of directly placing your code in the text field, create a custom field template for your view. Here’s how to do that:

Identify the Field: Determine the machine name of the field you are targeting.

Create a Template File: Create a file named views-view-field--[view-machine-name]--[field-machine-name].html.twig in your theme’s templates directory. Replace [view-machine-name] and [field-machine-name] with the respective names.

Place Your Code: In that file, place your HTML and Twig code:

twig

<div class="graph-container">
  <div class="graph-bar" style="width: {{ row.content['#row'].actual_time_needed }}%;" title="Actual Time Needed: {{ row.content['#row'].actual_time_needed }} hours;">
    Actual Time Needed ({{ row.content['#row'].actual_time_needed }} hours)
  </div>
  <div class="graph-bar" style="width: {{ row.content['#row'].tech_senior_time }}%;" title="Tech Senior Developer: {{ row.content['#row'].tech_senior_time }} hours;">
    Tech Senior Developer ({{ row.content['#row'].tech_senior_time }} hours)
  </div>
</div>
  1. Clear Cache

After creating or modifying your template files, clear the Drupal cache. You can do this by going to Configuration > Performance and clicking Clear all caches or by using Drush with the command:

bash

drush cr

  1. Inspect the Output

After clearing the cache, inspect the output using your browser’s developer tools. Check the style attribute of your graph bars to see if the widths are being populated as expected.
5. CSS Placement

Ensure that your CSS styles for .graph-container and .graph-bar are either included in the theme’s CSS files or inline within a tag in the custom template. If using inline styles, ensure they are placed before the graph’s HTML for better loading performance.
6. Debugging Values

If the values like {{ actual_time_needed }} or {{ tech_senior_time }} are not appearing as expected, make sure these values are being passed correctly in your view. You can use {{ dump(row.content) }} in your template to inspect what data is available in the $row variable.

To display external images as thumbnails for virtual posts without uploading them to the WordPress media library, you can leverage WordPress filters and hooks to manipulate how the thumbnail is displayed. The issue stems from how WordPress looks for image attachments in its own database. Since the external images aren’t stored locally, you need to inject them dynamically into the theme’s template.

Here’s how you can address this problem:

1. Override Thumbnail Display for Virtual Posts

You’re on the right track with using the post_thumbnail_html filter, but you might need a more refined approach to ensure the external image URL is injected properly.

Updated Solution Using post_thumbnail_html Filter

In your plugin, use the following code to dynamically inject the external image into the thumbnail slot without needing to upload it:

php

Copy code

add_filter('post_thumbnail_html', function ($html, $post_id, $post_thumbnail_id, $size, $attr) {
    global $post;

    // Check if this is a virtual post and if it has an external image URL
    if (isset($post->is_virtual) && $post->is_virtual && !empty($post->primaryImage)) {
        // Return an <img> tag with the external image URL
        $html = '<img src="' . esc_url($post->primaryImage) . '" class="wp-post-image" />';
    }

    return $html;
}, 10, 5);

2. Modify the Post Data

Ensure that your virtual post object contains a property like primaryImage that holds the URL of the external image. You already seem to have this part in place, but ensure that your virtual posts look something like this:

php

Copy code

$post = (object) [
    'ID' => 0, // Virtual post ID (no actual ID)
    'post_title' => 'Example Title',
    'post_excerpt' => 'This is a virtual post excerpt.',
    'post_date' => current_time('mysql'),
    'is_virtual' => true, // Flag to identify virtual posts
    'primaryImage' => 'https://external-site.com/image.jpg', // External image URL
];

3. Use the has_post_thumbnail() Filter

Since WordPress checks for thumbnails using has_post_thumbnail(), you should override this behavior for virtual posts by returning true if the post is virtual and has an external image URL:

php

Copy code

add_filter('has_post_thumbnail', function ($has_thumbnail, $post) {
    // Check if the post is virtual and has a primary image URL
    if (isset($post->is_virtual) && $post->is_virtual && !empty($post->primaryImage)) {
        return true; // Indicate that this virtual post has a thumbnail
    }

    return $has_thumbnail;
}, 10, 2);

4. Ensure External Images Are Displayed in Loops

Now, WordPress should display the external image as the post thumbnail in the post loop. To make sure your theme or front-page template uses the post_thumbnail_html filter and respects the external image, verify that the theme’s template files (e.g., content.php, archive.php) are using the_post_thumbnail() function correctly.

5. Additional Consideration: Custom Post Class or CSS

If you need to add custom CSS or further manipulate the virtual posts differently from regular posts, you can also add a custom class:

php

Copy code

add_filter('post_class', function ($classes, $class, $post_id) {
    global $post;

    // Add a custom class for virtual posts
    if (isset($post->is_virtual) && $post->is_virtual) {
        $classes[] = 'virtual-post';
    }

    return $classes;
}, 10, 3);