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

brij2136 created an issue. See original summary.

utcbrij’s picture

StatusFileSize
new149.38 KB
utcbrij’s picture

Issue summary: View changes
utcbrij’s picture

Issue summary: View changes
mustanggb’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.7.x-dev
Component: Views Data » views.module
Issue tags: -custom view, -custom module, -Needs title update, -custom template, -Drupal 8.x

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

cilefen’s picture

Status: Active » Closed (outdated)

I 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.