I have an entity that has a reference field on the Media Type. I take the reference field on Media Type to display images:
$storage = $this->entityTypeManager->getStorage('node');
$entity_product = $storage->load($entity_id);
$entity_product_media_ids = $entity_product->get('field_entity_product_media')->getValue();
And as a result, the $entity_product_media_ids variable contains:
Related
I have an Entity Product entity where one of the fields is:
reference field on Media type with name Media Product Image
Media Product Image has fields:
Image
Custom color id
And in the developers tools of the browser, the <img> looks like this:
<img loading="lazy src="/sites/default/files/styles...." width="300" height="550" typeof=foaf:Image>
I need to add the data-color attribute to the <img> tag and pass the value from the Custom Color id field to it.
This can be done with:
https://api.drupal.org/api/drupal/core%21includes%21theme.inc/function/template_preprocess_field/9.4.x
For example:
function my_theme_preprocess_field(&$variables, $hook) {
$element = $variables['element'];
if (isset($element['#field_name'])) {
if ($element['#field_name'] == 'field_image') {
// do something
}
}
}
But the problem is that when I write the word media in the .module file in my entity_product_admin module in the function name:
function entity_product_admin_preprocess_media(&$variables, $hook) {
}
I can't get the value of field_custom_color_id because there is no method in the API of the media entity with which I can get the value of this field:
Also, I don't have access to the image attributes and I can't add the data-color attribute with the value of the Custom color id field.
If I write the word image in the name of the function:
function entity_product_admin_preprocess_image(&$variables, $hook) {
}
Then I already have access to the image attributes, but I do not have access to the field_custom_color_id field:
I tried to write just field in the function name, but then I can get through anywhere.
How then should this function look like?
I've got a Typerocket form (within Wordpress) and I'm trying to make a conditional field.
Like if the Type field is Image, show and Image field afterwards and if the Type field is Quote have a text (editor) field afterwards.
I can't find anything about Conditional fields in Typerocket's doc.
Here's what I got right now.
echo $form->repeater('pj_images_du_projet')->setLabel("Images du projet")->setFields([
$form->select('Type')->setOptions([
"Image"=>'image',
"Vidéo"=>'video',
"Témoignage"=>'temoignage'
]), //Type is what chooses what fields are below. Options are Image, Video or Testimonial
$form->image('image')->setLabel('Image'), //image only
$form->text('video')->setLabel('Vidéo'), //video only
$form->toggle('autoplay')->setLabel('Autoplay'), //video only
$form->textarea('temoignage')->setLabel('Témoignage'), //temoignage only
$form->text('auteur')->setLabel('Auteur'), //temoignage only
]);
Created a JS that does the work.
JS
jQuery('body').ready(function(){
jQuery("body").on('change','.is-conditional-selector',function(){px_redoConditionals();})
px_redoConditionals();
})
function px_redoConditionals(){
jQuery('body').find('.is-conditional-selector').each(function(){
var repeater = jQuery(this).parent().parent().parent();
console.log(repeater.attr('class'));
repeater.find('.is-conditional').removeClass('px-show');
//console.log(jQuery(this).val());
repeater.find('.is-conditional.is-conditional-'+jQuery(this).val()).addClass('px-show');
})
}
CSS
.is-conditional:not(.px-show){
display:none !important;
}
And then I add this to the select that changes which section is shown
->setAttribute('class','is-conditional-selector')
And this on the fields that need to be shown or hidden
->setAttribute('class','is-conditional is-conditional-[value of the select to show this]'),
Theme
I have a content type with image and URL fields, I need to show image as banner on the path where URL field is matched with requested path using views.
I tried with
1-> adding "Alias" field as contextual filter in views.
2-> adding URL field
3-> I also tried with URL field with PHP Code in contextual filter:
if(drupal_is_front_page()) {
return '<front>';
}else{
return request_path();
}
3rd point works partially for only one path argument, like if current requested path is services/one and views contextual filter only takes first path component as you can see in attached image
However, I need to set contextual filter with whole path no matter how many components are requested.
How would I do that?
I have done this by embedding the views block in tlp and passing the URL field filter by code. See code below:
$path = request_path();
$query = 'SELECT field_url_url FROM {field_data_field_url}
WHERE bundle = :bundle AND entity_type = :entity_type AND field_url_url = :field_url_url';
$path = db_query($query, array(
':bundle'=>'page_banner',
':entity_type'=>'node',
':field_url_url'=> $path
))->fetchField();
if (!empty($path)):
print views_embed_view('page_banner','block', $path);
endif;
I hope this will help someone who needs to add contextual filter as request URL.
I have a grid image gallery built with view module.
And I want some effect for each image.
So,I try to add custom css class to each img tags.But I cannot it.
Please tell me "How to add custom css class in view module and grid format"?
You need to preprocess image style as follow:
/**
* THEME_preprocess_image_style() is also available.
*/
function <THEME_NAME>_preprocess_image(&$variables) {
if (isset($variables['style_name'])) {
if ($variables['style_name'] == 'thumbnail') {
$variables['attributes']['class'][] = "<YOUR CLASS NAME>";
}
}
}
Add above code in your template.php file of the current theme & clear cache.
Remember to replace text placed under <> in above code with appropriate values.
Hi this is simple no need to do this in coding, just do following.
1- Add a new field (Global Result counter) in your view and exclude it from display.
2- Exclude your image field from display.
3- Add a new field Global Custom text field, here you can add token of field Result counter to your image field, like following.
<div class="Image-[put-result-counter-token-here]" > [token-for-image-field]</div>
This will output a dynamic class for your image field.
I have added a text field (field_title_alt) to a node type.
If the field_title_alt is edited, I want to replace the value of the default node title with the value of field_title_alt.
I have tried the following:
preprocess_node
hook_node_view
hook_node_load
The problem is that when I try to override $node->title, the value is not changed when being outputtet. I have access to the title in the $node->content, but it has markup. I only want to replace the value.
Your code should be in hook_preprocess_node() but you shouldn't set $node->title as the title has already been set up in the template vars. Try this:
function MYMODULE_preprocess_node(&$vars) {
if ($a_condition) {
$vars['title'] = 'A new title';
}
}