reactjs_mount-8.x-1.0-alpha2/reactjs_mount.module
reactjs_mount.module
<?php /** * @file * Contains reactjs_mount.module. */ use Drupal\node\Entity\Node; use Drupal\Component\Utility\Html; use Drupal\Core\Routing\RouteMatchInterface; /** * Implements hook_help(). */ function reactjs_mount_help($route_name, RouteMatchInterface $route_match) { switch ($route_name) { // Main module help for the reactjs_mount module. case 'help.page.reactjs_mount': $output = ''; $output .= '<h3>' . t('About') . '</h3>'; $output .= '<p>' . t('Module for mounting ReactJS to a node') . '</p>'; return $output; default: } } /** * Implements hook_theme(). */ function reactjs_mount_theme($existing, $type, $theme, $path) { return [ 'reactjs_mount' => [ 'render element' => 'children', ], 'node__reactjs_mount' => [ 'template' => 'node--reactjs-mount', 'preprocess functions' => ['reactjs_mount_preprocess_node'], ], ]; } /** * Implements hook_preprocess_node() */ function reactjs_mount_preprocess_node(&$vars) { // Make sure it's the right content type. if ($vars['elements']['#node']->getType() == 'react_mount_node') { // Get the nid to load the node and get the content. $nid = $vars['elements']['#node']->id(); $node = Node::load($nid); // Get the value for the set ID. $div_id_field = $node->get('field_div_id'); $div_id = $div_id_field->getValue(); // Set the ID to the vars array to use in the twig. $vars['div_id'] = Html::escape($div_id[0]['value']); // Get the attached library from the node. $lib_field = $node->get('field_attached_library'); $lib = $lib_field->getValue(); if (isset($lib[0]['value'])) { $vars['my_lib'] = Html::escape($lib[0]['value']); } else { drupal_set_message(t('No library has been attached to this node'), 'error'); } } } /** * Implements hook_theme_suggestions_HOOK_alter() for node templates. */ function reactjs_mount_theme_suggestions_node_alter(array &$suggestions, array $variables) { // Only force this template if it's a content type built for react. if ($variables['elements']['#node']->getType() == 'react_mount_node') { $suggestions[] = 'node__reactjs_mount'; } }