I'm using Drupal 8.6.9 and Commerce 2.0x with custom code.
I'm trying to add a product title under the image in a product-filter type of grid. (I've attached an image of the page and of the view.) I'm hoping someone much more savvy than me can show me a simple fix here. The code is found in a custom module and the accompanying html.twig file.
Here are the module's functions:
function EXAMPLE_theme() {
return [
'EXAMPLE_view_tabs_content_transfer' => [
'variables' => [
'path' => '',
'url' => '',
],
],
];
}
function EXAMPLE_view_tabs_transfer(&$vars, $category_field_name, $view_id, $view_display = '') {
$view_tabs = [];
foreach ($vars['view']->result as $id => $result) {
$fields = $result->_entity->getFields();
if (array_key_exists($category_field_name, $fields)) {
$field_values = $fields[$category_field_name]->getValue();
if (!empty($field_values)) {
$names = [];
foreach ($field_values as $value) {
$term = Term::load($value['target_id']);
if ($term) {
$category_name = $term->get('name')->value;
$key = [
$view_id,
$view_display,
$value['target_id'],
];
$cid = implode('-', $key);
$names[] = $cid;
$view_tabs[$cid] = $category_name;
}
}
$vars['rows'][0]['#rows'][$id]['#options']['tab_classes'] = implode(' ', $names);
}
}
}
asort($view_tabs);
$vars['rows'][0]['#rows']['view_tabs']['#tabs'] = $view_tabs;
}
/**
* @param $vars
*/
function EXAMPLE_view_tabs_content_transfer(&$vars, $category_field_name, $image_field_name, $view_id, $view_display = '') {
$view_tabs_content = [];
foreach ($vars['view']->result as $id => $result) {
$entity = $result->_entity;
$fields = $entity->getFields();
if (array_key_exists($category_field_name, $fields)) {
$field_values = $fields[$category_field_name]->getValue();
$url = '#';
if ($entity->hasVariations()) {
$variations = $entity->getVariations();
$variation = reset($variations);
$variation_fields = $variation->getFields();
if (array_key_exists($image_field_name, $variation_fields)) {
if ($variation_fields[$image_field_name]->getFieldDefinition()->getType() == 'image') {
$fid = $variation_fields[$image_field_name]->target_id;
$uri = File::load($fid)->getFileUri();
$url = file_create_url($uri);
}
}
}
if (!empty($field_values)) {
foreach ($field_values as $value) {
$key = [
$view_id,
$view_display,
$value['target_id'],
];
$cid = implode('-', $key);
$entity_type_id = $entity->getEntityTypeId();
switch ($entity_type_id) {
case 'commerce_product':
$entity_type = 'product';
break;
default:
$entity_type = $entity_type_id;
}
$path = \Drupal::service('path.alias_manager')->getAliasByPath('/' . $entity_type . '/' . $entity->id());
$theme_array = [
'#theme' => 'EXAMPLE_view_tabs_content_transfer',
'#path' => base_path() . ltrim($path, '/'),
'#url' => $url,
];
$view_tabs_content[$cid][] = \Drupal::service('renderer')->renderPlain($theme_array);
}
}
}
}
ksort($view_tabs_content);
$vars['rows'][0]['#rows']['view_tabs_content']['#tabs_content'] = $view_tabs_content;
}
function EXAMPLE_cms_preprocess_views_view(&$variables) {
$vid = $variables['view']->id();
$current_display = $variables['view']->current_display;
switch ($vid) {
case 'project':
switch ($current_display) {
case 'full_width_gallery':
case 'extended_portfolio':
case 'masonry_gallery':
case 'classic_porfolio_page':
case 'page_2col_portfolio':
case 'page_index_gallery':
case 'page_lightbox_gallery':
case 'page_masonry_portfolio':
case 'page_3':
case 'page_3_2':
case 'isotope_grid':
case 'isotope_grid_overlay':
break;
case 'products_filter':
EXAMPLE_cms_view_tabs_transfer($variables, 'field_product_category', $vid, $current_display);
EXAMPLE_cms_view_tabs_content_transfer($variables, 'field_product_category', 'field_images',$vid, $current_display);
break;
}
}
Here is the code in the accompanying twig file:
<div class="c-overlay-wrapper c-overlay-padding">
<div class="c-overlay-content">
<a href="{{ path }}" class="btn btn-md c-btn-grey-1 c-btn-uppercase c-btn-bold c-btn-border-1x c-btn-square">
{{ 'Explore'|t }}
</a>
</div>
</div>
<div class="c-bg-img-center c-overlay-object" data-height="height" style="height: 270px; background-image: url(' {{ url }} ');"></div>
Comments
Comment #2
utcbrij commentedComment #3
utcbrij commentedComment #4
utcbrij commentedComment #5
mustanggb commentedComment #12
cilefen commentedI am closing this support request because there have been no recent comments.
The Drupal Core issue queue is not the ideal place for support requests. Consider other sources of support.