[ 'variables' => [ 'addtoany_html' => FALSE, 'link_url' => FALSE, 'link_title' => FALSE, 'buttons_size' => FALSE, 'button_setting' => FALSE, 'button_image' => FALSE, 'universal_button_placement' => FALSE, 'entity_type' => '', 'bundle' => '', ], ], ]; } /** * Implements hook_theme_suggestions_HOOK(). */ function addtoany_theme_suggestions_addtoany_standard(array $variables) { $suggestion = 'addtoany_standard__'; $suggestions = []; if (!empty($variables['entity_type'])) { $suggestion .= $variables['entity_type']; $suggestions[] = $suggestion; } if (!empty($variables['entity_type']) && !empty($variables['bundle'])) { $suggestion .= '__' . $variables['bundle']; $suggestions[] = $suggestion; } return $suggestions; } /** * Implements hook_entity_extra_field_info(). */ function addtoany_entity_extra_field_info() { $extra = []; $types = ['node', 'comment', 'media']; // Allow modules to alter the entity types. \Drupal::moduleHandler()->alter('addtoany_entity_types', $types); foreach ($types as $type) { $bundles = Drupal::service('entity_type.bundle.info')->getBundleInfo($type); foreach ($bundles as $bundle => $bundle_data) { $extra[$type][$bundle]['display']['addtoany'] = [ 'label' => t('AddToAny'), 'description' => t('Share buttons by AddToAny'), 'weight' => 5, 'visible' => FALSE, ]; } } return $extra; } /** * Implements hook_ENTITY_TYPE_view(). */ function addtoany_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { if ($display->getComponent('addtoany')) { $data = addtoany_create_entity_data($entity); $build['addtoany'] = [ '#addtoany_html' => \Drupal::token()->replace($data['addtoany_html'], ['node' => $entity]), '#link_url' => $data['link_url'], '#link_title' => $data['link_title'], '#button_setting' => $data['button_setting'], '#button_image' => $data['button_image'], '#universal_button_placement' => $data['universal_button_placement'], '#buttons_size' => $data['buttons_size'], '#entity_type' => $entity->getEntityType()->id(), '#bundle' => $entity->bundle(), '#theme' => 'addtoany_standard', ]; } } /** * Implements hook_page_attachments(). */ function addtoany_page_attachments(&$page) { $config = \Drupal::config('addtoany.settings'); // Initial JavaScript. $additional_js = $config->get('additional_js'); if (\Drupal::moduleHandler()->moduleExists('token')) { $node = \Drupal::routeMatch()->getParameter('node'); $data = []; if ($node) { if (is_numeric($node)) { $node = Node::load($node); } $data = ['node' => $node]; $additional_js = \Drupal::token()->replace($additional_js, $data); } } $javascript_header = 'window.a2a_config=window.a2a_config||{};' . 'a2a_config.callbacks=[];a2a_config.overlays=[];' . 'a2a_config.templates={};' . (($config->get('no_3p')) ? 'a2a_config.no_3p=1;' : '') . $additional_js; // Add AddToAny initial JS config. $page['#attached']['html_head'][] = [ // The data. [ // Add a