/** * {@inheritdoc} * * @see ::prepareView() * @see ::getEntitiestoView() */ public function view(FieldItemListInterface $items, $langcode = NULL) { $elements = parent::view($items, $langcode); $field_level_access_cacheability = new CacheableMetadata(); // Try to map the cacheability of the access result that was set at // _accessCacheability in getEntitiesToView() to the corresponding render // subtree. If no such subtree is found, then merge it with the field-level // access cacheability. foreach ($items as $delta => $item) { // Ignore items for which access cacheability could not be determined in // prepareView(). if (!empty($item->_accessCacheability)) { if (isset($elements[$delta])) { CacheableMetadata::createFromRenderArray($elements[$delta])->merge($item->_accessCacheability)->applyTo($elements[$delta]); } else { $field_level_access_cacheability = $field_level_access_cacheability->merge($item->_accessCacheability); } } } // Apply the cacheability metadata for the inaccessible entities and the // entities for which the corresponding render subtree could not be found. // This causes the field to be rendered (and cached) according to the cache // contexts by which the access results vary, to ensure only users with // access to this field can view it. It also tags this field with the cache // tags on which the access results depend, to ensure users that cannot view // this field at the moment will gain access once any of those cache tags // are invalidated. $field_level_access_cacheability->applyTo($elements); return $elements; }
/** * This method is the wrapper for the formatter. */ public function view(FieldItemListInterface $items, $langcode = NULL) { $elements = parent::view($items); $types = $this->getRemoteTypes(); $elements['#prefix'] = '<p>The cover source is: ' . $types[$this->settings['cover_source']] . '</p>'; return $elements; }