@sachinsnimbalkar@gmail.com

Account created on 19 December 2023, 9 months ago
#

Recent comments

I have created the twig file based on my content type template/field/field--node--field-article-video--article.html.twig
with following code and
{#
/**
* @file
* Default template for a field.
*/
#}

{% if gallery_video_thumbURL %}

{% else %}

{% for item in items %}
{{ item.content }}
{% endfor %}

{% endif %}

In my theme file created function
function extractYouTubeVideoID($url) {
// Regular expression pattern to match YouTube video IDs
$pattern = '/(?<=watch\?v=|\/videos\/|embed\/|youtu.be\/|\/v\/|\/e\/|watch\?v%3D|\?v%3D)([\w-]{11})/i';

// Perform the regular expression match
preg_match($pattern, $url, $matches);

// Check if the video ID is found and return it, or return null if not found
return isset($matches[1]) ? $matches[1] : null;
}
function crystalrun_preprocess_field(&$variables) {
$entity = \Drupal::routeMatch()->getParameter('node');

if ($entity instanceof \Drupal\node\NodeInterface) { // Check if it is a node
$nid = $entity->id(); // You also have the node object right here$

$node = \Drupal\node\Entity\Node::load($nid);
$content_type= $node->getType();
/**********/
if ($content_type == "article"){
// check the field value exits or not.
$url = $node->field_article_video->getValue();
if (!empty($url))
{
$field_value = $node->field_article_video->entity->field_media_oembed_video->getValue()[0]['value'];
$videoID = extractYouTubeVideoID($field_value);
$variables['gallery_video_thumbURL'] = 'https://www.youtube.com/embed/'.$videoID ? 'https://www.youtube.com/embed/'.$videoID :'' ;
}else
{
$variables['gallery_video_thumbURL']="";
}
}
/*********/
}

Its absolutely working fine for me, please share you feedback and comment on the resolution, can be used with other contenttype simillary be creating twig file and handling the logic.

Production build 0.71.5 2024