Skip to content

Commit

Permalink
Code formatting fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicdreams committed May 17, 2018
1 parent 8beb646 commit 174d014
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 175 deletions.
5 changes: 5 additions & 0 deletions hax.install
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

/**
* @file
* Hax install hooks.
*/

/**
* Implements hook_install().
*/
Expand Down
85 changes: 46 additions & 39 deletions hax.module
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

/**
* @file
* Headless Authoring eXperience, all we do is accept calls and make things
* haxable in the first place.
* Headless Authoring eXperience, accept calls and make things haxable.
*/

use Drupal\hax\HaxService;

/**
* Implememts hook_preprocess_hook().
*
Expand All @@ -14,7 +15,6 @@
function hax_preprocess_field(&$vars) {

// Supporting attachments handled in hax_page_attachments().

// Allow for cache variability based on route name so this can be handled
// evaluated separately for the view and the hax mode tabs.
$vars['#cache']['contexts'][] = 'route.name';
Expand All @@ -24,46 +24,40 @@ function hax_preprocess_field(&$vars) {
}

$entity_type = $vars['element']['#object']->getEntityTypeId();
$bundle = $vars['element']['#object']->bundle();
$view_mode = $vars['element']['#view_mode'];

if ($vars['field_name'] == 'body' && $entity_type == 'node' && $view_mode == 'full'
&& \Drupal::config('hax.settings')->get('hax_autoload_element_node_view')) {

// Generate autoload list.
$elementstring = \Drupal::config('hax.settings')->get('hax_autoload_element_list');
$elementstring = \Drupal::config('hax.settings')
->get('hax_autoload_element_list');
// Blow up based on space.
$elements = explode(' ', $elementstring);
$components = '';
foreach ($elements as $element) {
// sanity check
if (!empty($element)) {
$components .= '<' . $element . ' slot="autoloader">' . '</' . $element . '>';
}
}

$appStoreConnection = json_encode(['url' => base_path() . 'hax-app-store/' . \Drupal::csrfToken()->get()]);
$appStoreConnection = json_encode([
'url' => base_path() . 'hax-app-store/' . \Drupal::csrfToken()->get(),
]);

// Get the NID from current path.
$request = \Drupal::request();
$current_path = $request->getPathInfo();
$path_args = explode('/', $current_path);
$node_id = $path_args[2];

// Add <cms-hax> tag around body markup.

// DRUPAL is stripping tag in #prefix and #suffix...
// see Drupal\Component\Utility\Xss
//$vars['items'][0]['content']['#prefix'] = '
// <cms-hax open-default end-point="' . base_path() . 'hax-node-save/' . $node_id . '/' . \Drupal::csrfToken()->get() . '" body-offset-left="' . \Drupal::config('hax.settings')->get('hax_offset_left') . '" app-store-connection=' . "'" . json_encode($appStoreConnection) . "'" . '>'
// . $components;
//$vars['items'][0]['content']['#suffix'] = '</cms-hax>';

// TODO Not ideal, but we've fallen back to using the body text instead of
// prefix/suffix due to filtering. This will go through the node's selected
// text format for render.
$vars['items'][0]['content']['#text'] = '
<cms-hax open-default end-point="' . base_path() . 'hax-node-save/' . $node_id . '/' . \Drupal::csrfToken()->get() . '" body-offset-left="' . \Drupal::config('hax.settings')->get('hax_offset_left') . '" app-store-connection=' . "'" . $appStoreConnection . "'" . '>'
// @todo: convert to string replacement function or template.
$vars['items'][0]['content']['#text'] = '<cms-hax open-default end-point="' . base_path() . 'hax-node-save/' . $node_id . '/' . \Drupal::csrfToken()
->get() . '" body-offset-left="' . \Drupal::config('hax.settings')
->get('hax_offset_left') . '" app-store-connection=' . "'" . $appStoreConnection . "'" . '>'
. $components . $vars['items'][0]['content']['#text'] . '</cms-hax>';

}
Expand All @@ -77,8 +71,8 @@ function hax_hax_app_store() {

$config = \Drupal::config('hax.settings');

$hax = new \Drupal\hax\HAXService;
$apikeys = array();
$hax = new HaxService();
$apikeys = [];
$baseApps = $hax->baseSupportedApps();
foreach ($baseApps as $key => $app) {

Expand All @@ -97,17 +91,19 @@ function hax_hax_app_store() {
}

/**
* Connection details for this site. This is where
* all the really important stuff is that will
* make people freak out.
* Hax site connection.
*
* Connection details for this site. This is where all the really important
* stuff is that will make people freak out.
*/
function _hax_site_connection() {
global $base_url;
$parts = explode('://', $base_url);
// built in support when file_entity and restws is in place
// Built in support when file_entity and restws is in place.
$title = t('Internal files');
$json = '{
"details": {
"title": "' . t('Internal files') . '",
"title": "' . $title . '",
"icon": "perm-media",
"color": "light-blue",
"author": "Drupal",
Expand Down Expand Up @@ -183,52 +179,65 @@ function _hax_site_connection() {
*
* Load all attachments for this page.
*/

function hax_page_attachments(array &$attachments) {

// Need this to execute on variations of route.name to support view and
// hax mode tab variation.
// @todo: As written, this code has no impact. Need to impact $attachments.
$vars['#cache']['contexts'][] = 'route.name';

if (!hax_is_current_route_supported()) {
return;
}

// Fake a component to get it into the head of the document, heavy weighting
// Fake a component to get it into the head of the document, heavy weighting.
$component = new \stdClass();
$component->machine_name = 'cms-hax';
$component->file = 'libraries/webcomponents/polymer/bower_components/cms-hax/cms-hax.html';

// The #tag is the html tag.
// Set up an array of attributes inside the tag.
$element = [
'#tag' => 'link', // The #tag is the html tag
'#attributes' => [ // Set up an array of attributes inside the tag
'#tag' => 'link',
'#attributes' => [
'href' => base_path() . $component->file,
'rel' => 'import',
],
];
$attachments['#attached']['html_head'][] = [$element, 'webcomponent-' . $component->machine_name];
$attachments['#attached']['html_head'][] = [
$element,
'webcomponent-' . $component->machine_name,
];

// TODO do we need the autoload attachments here, too?

// Generate autoload list.
$elementstring = \Drupal::config('hax.settings')->get('hax_autoload_element_list');
$element_string = \Drupal::config('hax.settings')
->get('hax_autoload_element_list');

// Blow up based on space.
$elements = explode(' ', $elementstring);
$components = '';
$elements = explode(' ', $element_string);

foreach ($elements as $element) {
$component = new \stdClass();
$component->machine_name = $element;
// Pull in from webcomponents location.
$component->file = 'libraries/webcomponents/polymer/bower_components/' . $element . '/' . $element . '.html';

// The #tag is the html tag.
// Set up an array of attributes inside the tag.
$element = [
'#tag' => 'link', // The #tag is the html tag
'#attributes' => [ // Set up an array of attributes inside the tag
'#tag' => 'link',
'#attributes' => [
'href' => base_path() . $component->file,
'rel' => 'import',
],
];

// Add components to html_head via $vars.
$attachments['#attached']['html_head'][] = [$element, 'webcomponent-' . $component->machine_name];
$attachments['#attached']['html_head'][] = [
$element,
'webcomponent-' . $component->machine_name,
];
}

}
Expand All @@ -249,5 +258,3 @@ function hax_is_current_route_supported() {

return FALSE;
}


14 changes: 7 additions & 7 deletions hax.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ hax.node_save:
methods: [PUT]
defaults:
op: update
_controller: '\Drupal\hax\Controller\HaxModeController::_hax_node_save'
_controller: '\Drupal\hax\Controller\HaxModeController::haxNodeSave'
requirements:
_custom_access: '\Drupal\hax\Controller\HaxModeController::_hax_node_access'
_custom_access: '\Drupal\hax\Controller\HaxModeController::haxNodeAccess'
hax.file_save:
path: '/hax-file-save/{token}'
defaults:
op: create
_controller: '\Drupal\hax\Controller\HaxModeController::_hax_file_save'
_controller: '\Drupal\hax\Controller\HaxModeController::haxFileSave'
requirements:
_custom_access: '\Drupal\hax\Controller\HaxModeController::_hax_file_access'
_custom_access: '\Drupal\hax\Controller\HaxModeController::haxFileAccess'
hax.load_app_store:
path: '/hax-app-store/{token}'
defaults:
_controller: '\Drupal\hax\Controller\HaxModeController::_hax_load_app_store'
_controller: '\Drupal\hax\Controller\HaxModeController::haxLoadAppStore'
requirements:
_permission: 'use hax'
hax.node_form:
path: '/node/{node}/hax'
defaults:
op: update
_title_callback: '\Drupal\hax\Controller\HaxModeController::title'
_controller: '\Drupal\hax\Controller\HaxModeController::hax_node_form'
_controller: '\Drupal\hax\Controller\HaxModeController::haxNodeForm'
requirements:
_custom_access: '\Drupal\hax\Controller\HaxModeController::_hax_node_access'
_custom_access: '\Drupal\hax\Controller\HaxModeController::haxNodeAccess'
hax.settings:
path: /admin/config/content/hax
defaults:
Expand Down
Loading

0 comments on commit 174d014

Please sign in to comment.