Okay. Current version of metatag_views does not support views data in it's tokens. The idea is to make custom handler based on Custom text, where theme() method will have the rendered text. If I put the result in drupal_static variable, I can get it in hook_page_attachments. But if the view is cached, hook_page_attachments does not gave a value.

Is there a way to save the data generated in the field handler and get it when the view is displayed? Or is there a way to get cached view with some hook and get the value from its results?

Handler code. Copy and paste from «Custom text»:

 * A handler to provide a field that is completely custom by the administrator.
 * @ingroup views_field_handlers
 * @ViewsField("description_field")
class DescriptionField extends FieldPluginBase {
   * {@inheritdoc}
  public function render(ResultRow $values) {
    // Return the text, so the code never thinks the value is empty.
    $ret = ViewsRenderPipelineMarkup::create(Xss::filterAdmin($this->options['alter']['text']));
    // $ret has rendered value. I need to store it somewhere.
    $description = &drupal_static('bd_metatag_description');
    $description = $ret;
    return $ret;

And then hook:

function HOOK_page_attachments(array &$page) {
  $d = &drupal_static('bd_metatag_description');
  // When the view is not cached everything is okay. But is there a way to get cached view data?
    $description = [
      '#tag' => 'meta',
      '#attributes' => [
        'name' => 'description',
        'content' => $d,
    $page['#attached']['html_head'][] = [$description, 'description'];

  • You seem to have some code that you have tried, perhaps you can post it and explain what did not work. – GiorgosK Mar 27 at 19:24
  • I've updated the question. – menelai Mar 27 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.