How do I assign variables from FormatterBase-Interface to hook_page_attachments()? I have created new field-types in drupal.

Therefore I have an Instance of FormatterBase, which gets the values from the database and tries to render the values. 

but i want to pass the variables #wkt and #zoom also to the _page_attachments-hook of the .module file, because I need the variables in the drupalsettings variable. (js neededs this variables)

class GeoObjectWKTFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items) {
    $elements = array();
    foreach ($items as $delta =&gt; $item) {
      $elements[$delta] = array(
        '#theme' =&gt; 'myosm_block',
        '#wkt' =&gt; SafeMarkup::checkPlain($item-&gt;value),
        '#zoom' =&gt; SafeMarkup::checkPlain($item-&gt;zoom_level),
      );
    }
    return $elements;
  }
}


My hook:

/**
 * Implements hook_page_attachments().
 */
function mymodule_page_attachments(array &amp;$attachments) {
    $attachments['#attached']['drupalSettings']['mymodule']['zoom'] = //here I want to assign the value;
    $attachments['#attached']['drupalSettings']['mymodule']['wkt'] = //here I want to assign the value;
}
No, you don't want to do this.

You can set #attached everywhere. hook_page_attachments() is just the global hook that allows to add attached things to all pages.

But you don't need this if you want to use #attached for own structures, just add them to your render array that you return.