Created
February 7, 2017 15:41
-
-
Save cyberlex404/ee3f52df70f6caeceb1567e2ee6265e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @file | |
| * Contains metatag_custom_route.module. | |
| */ | |
| use Drupal\Core\Routing\RouteMatchInterface; | |
| use Drupal\metatag_custom_route\Entity\MetatagRouteInterface; | |
| /** | |
| * Implements hook_help(). | |
| */ | |
| function metatag_custom_route_help($route_name, RouteMatchInterface $route_match) { | |
| switch ($route_name) { | |
| // Main module help for the metatag_custom_route module. | |
| case 'help.page.metatag_custom_route': | |
| $output = ''; | |
| $output .= '<h3>' . t('About') . '</h3>'; | |
| $output .= '<p>' . t('My Awesome Module') . '</p>'; | |
| return $output; | |
| default: | |
| } | |
| } | |
| /** | |
| * Implements hook_page_attachments_alter(). | |
| */ | |
| function metatag_custom_route_page_attachments_alter(array &$attachments) { | |
| $metatag_attachments = metatag_custom_route_get_metatag(); | |
| if (!$metatag_attachments) { | |
| return; | |
| } | |
| // If any Metatag items were found, append them. | |
| if (!empty($metatag_attachments['#attached']['html_head'])) { | |
| if (empty($attachments['#attached'])) { | |
| $attachments['#attached'] = []; | |
| } | |
| if (empty($attachments['#attached']['html_head'])) { | |
| $attachments['#attached']['html_head'] = []; | |
| } | |
| foreach ($metatag_attachments['#attached']['html_head'] as $item) { | |
| $attachments['#attached']['html_head'][] = $item; | |
| } | |
| } | |
| } | |
| function metatag_custom_route_get_metatag() { | |
| $metatag_manager = \Drupal::service('metatag.manager'); | |
| $route_match = \Drupal::routeMatch(); | |
| $route_name = $route_match->getRouteName(); | |
| //dpm($route_name); | |
| // First, get defaults. | |
| $metatags = metatag_get_default_tags(); | |
| // Get metatags of entity | |
| $query = \Drupal::entityQuery('metatag_route') | |
| ->condition('name', $route_name); | |
| $metatag_route_storage = \Drupal::entityManager()->getStorage('metatag_route'); | |
| $mrids = $query->execute(); | |
| if(!empty($mrids)) { | |
| $entity = $metatag_route_storage->load(reset($mrids)); | |
| }else { | |
| return; | |
| } | |
| if (!empty($entity) && $entity instanceof MetatagRouteInterface) { | |
| foreach ($metatag_manager->tagsFromEntity($entity) as $tag => $data) { | |
| $metatags[$tag] = $data; | |
| } | |
| } | |
| return $metatag_manager->generateElements($metatags, $entity); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment