The variable $user_current which is an instance of AccountProxyInterface is declared protected and used as per the arguments mentioned in the services.yml file. Here the Summar圜acheContext class implements the interface CacheContextInterface. $summary = $user_details->get('field_summary')->getValue() The summary field should be used to create this cache_context logic as per the following code snippet: We need to tag this service to cache_context as well. The detail on how to pass a service as an argument to another is available at. The service takes in the current_user service as an argument. As per the above snippet, the code for cache context goes into src/CacheContext/UserSummar圜acheContext.php. Similarly, we can define a further level of hierarchy as well. The name of the service should be of the format cache_context.* i.e should start with ‘cache_context.’ followed by the appropriate name. The trick here is to understand the naming convention for a new cache context. Prerequisite: Add a field for filling in summary in the user edit form(/user//edit) provided by default in drupal.Ĭache context can be registered as any other service in the file: services:Ĭlass: Drupal\example_cache_context\CacheContext\UserSummar圜acheContext Let us consider a simple example to invalidate the cache of a block that displays a personalised message based on the summary that the user has provided in the user edit page. Our main focus here would be how to define and use a custom cache_context according to our requirement. In such a way we can explicitly decide as to when the cache of the element will be invalidated based on the context.ĭ8 core provides few predefined cache contexts that are available at . When an anonymous user comes to the same page the entire request is carried out and the display with limited access to links will be shown. Hereafter if another authenticated user visits the page the cached version of the block will be served thereby enhancing the site performance. When an authenticated user hits the page, the version of the block with access to all links will be displayed. For simplicity let us assume that there exist only two roles authenticated and anonymous. Hence ‘user.roles’ can be used as a cache context in such a scenario. This data completely depends upon the role of the user. Now authorised users will be given access to all the links while anonymous ones will be provided only with the free tutorials. Here is a guide that helps you easily grab in some basic concepts of Cache Context.Ĭache Context is basically a service that helps in creating multiple cached versions of something depending upon the context/request be it a view, block or any other section on the page.įor instance, let us consider a block displaying a list of tutorial links on a D8 instance. In one of our earlier posts, we have exemplified Cache tags. Still, this topic remains unrevealed to many developers as they consider caching to be a critical aspect of a website. We are well aware of the fact that Drupal Cache API is a remarkable feature introduced in Drupal 8.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |