getListCacheContexts example

/** * {@inheritdoc} */
  public function getCacheTags() {
    return Cache::mergeTags(parent::getCacheTags()$this->entityTypeManager->getDefinition('workflow')->getListCacheTags());
  }

  /** * {@inheritdoc} */
  public function getCacheContexts() {
    return Cache::mergeContexts(parent::getCacheContexts()$this->entityTypeManager->getDefinition('workflow')->getListCacheContexts());
  }

  /** * {@inheritdoc} */
  public function getValueOptions() {
    if (isset($this->valueOptions)) {
      return $this->valueOptions;
    }
    $this->valueOptions = [];

    

  public function render() {
    $build['table'] = [
      '#type' => 'table',
      '#header' => $this->buildHeader(),
      '#title' => $this->getTitle(),
      '#rows' => [],
      '#empty' => $this->t('There are no @label yet.', ['@label' => $this->entityType->getPluralLabel()]),
      '#cache' => [
        'contexts' => $this->entityType->getListCacheContexts(),
        'tags' => $this->entityType->getListCacheTags(),
      ],
    ];
    foreach ($this->load() as $entity) {
      if ($row = $this->buildRow($entity)) {
        $build['table']['#rows'][$entity->id()] = $row;
      }
    }

    // Only add the pager if a limit is specified.     if ($this->limit) {
      
$container->get('entity_type.manager')
    );
  }

  /** * {@inheritdoc} */
  public function build() {
    $build = [
      'form' => $this->formBuilder->getForm(WorkspaceSwitcherForm::class),
      '#cache' => [
        'contexts' => $this->entityTypeManager->getDefinition('workspace')->getListCacheContexts(),
        'tags' => $this->entityTypeManager->getDefinition('workspace')->getListCacheTags(),
      ],
    ];
    return $build;
  }

}

    // Always associate the list cache tag, otherwise the cached empty result     // wouldn't be invalidated. This would continue to show nothing matches the     // query, even though a newly created entity might match the query.     $cache_tags = Cache::mergeTags($cache_tags$entity_type_definition->getListCacheTags());

    return [
      '#theme' => 'item_list',
      '#items' => $labels,
      '#title' => $entity_type_id . ' entities',
      '#cache' => [
        'contexts' => $entity_type_definition->getListCacheContexts(),
        'tags' => $cache_tags,
      ],
    ];
  }

  /** * Empty list of entities of the given entity type. * * Empty because no entities match the query. That may seem contrived, but it * is an excellent way for testing whether an entity's list cache tags are * working as expected. * * @param string $entity_type_id * The type of the entity being listed. * * @return array * A renderable array. */
// Setup base information of the views data.     $data[$base_table]['table']['group'] = $this->entityType->getLabel();
    $data[$base_table]['table']['provider'] = $this->entityType->getProvider();

    $views_base_table = $base_table;
    if ($data_table) {
      $views_base_table = $data_table;
    }
    $data[$views_base_table]['table']['base'] = [
      'field' => $entity_id_key,
      'title' => $this->entityType->getLabel(),
      'cache_contexts' => $this->entityType->getListCacheContexts(),
      'access query tag' => $this->entityType->id() . '_access',
    ];
    $data[$base_table]['table']['entity revision'] = FALSE;

    if ($label_key = $this->entityType->getKey('label')) {
      if ($data_table) {
        $data[$views_base_table]['table']['base']['defaults'] = [
          'field' => $label_key,
          'table' => $data_table,
        ];
      }
      
return Cache::PERMANENT;
  }

  /** * {@inheritdoc} */
  public function getCacheContexts() {
    $contexts = [];
    if (($views_data = Views::viewsData()->get($this->view->storage->get('base_table'))) && !empty($views_data['table']['entity type'])) {
      $entity_type_id = $views_data['table']['entity type'];
      $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
      $contexts = $entity_type->getListCacheContexts();
    }
    return $contexts;
  }

  /** * {@inheritdoc} */
  public function getCacheTags() {
    return [];
  }

  
'#type' => 'container',
      '#attributes' => [
        'class' => 'workspaces',
      ],
    ];
    $build['workspaces_list']['workspaces'] = [
      '#theme' => 'item_list',
      '#title' => $this->t('Other workspaces:'),
      '#items' => $items,
      '#wrapper_attributes' => ['class' => ['workspaces__list']],
      '#cache' => [
        'contexts' => $this->entityType->getListCacheContexts(),
        'tags' => $this->entityType->getListCacheTags(),
      ],
    ];
    $build['workspaces_list']['all_workspaces'] = [
      '#type' => 'link',
      '#title' => $this->t('View all workspaces'),
      '#url' => Url::fromRoute('entity.workspace.collection'),
      '#attributes' => [
        'class' => ['all-workspaces'],
      ],
    ];
    
Home | Imprint | This part of the site doesn't use cookies.