Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-237: Move ADO Tools into this module, make JMESPATH a subsection + add a view for listing children #238

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
729 changes: 729 additions & 0 deletions config/optional/views.view.ado_tools_children.yml

Large diffs are not rendered by default.

730 changes: 730 additions & 0 deletions config/optional/views.view.ado_tools_children_creative_work_series.yml

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions js/jmespath-codemirror_strawberryfield.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(function ($, Drupal) {
Drupal.AjaxCommands.prototype.strawberryfield_codemirror = function (ajax, response, status) {
if (!window.CodeMirror) {
return;
}

$editors = $(response.selector).find('.CodeMirror');

if (response.hasOwnProperty('content') &&
$editors.length > 0 ) {
console.log('we have content');
$editors[0].CodeMirror.setValue(response.content);
}
};

})(jQuery, Drupal);
51 changes: 51 additions & 0 deletions src/Ajax/UpdateCodeMirrorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Created by PhpStorm.
* User: dpino
* Date: 10/1/19
* Time: 11:30 AM
*/

namespace Drupal\strawberryfield\Ajax;
use Drupal\Core\Ajax\CommandInterface;
aksm marked this conversation as resolved.
Show resolved Hide resolved

class UpdateCodeMirrorCommand implements CommandInterface {

/**
* The Content that will be updated on the Code Mirror text area
*
* @var \stdClass;
*/
protected $content;

/**
* The JQuery() selector
*
* @var string
*/
protected $selector;

/**
* Constructs an AlertCommand object.
*
* @param string $text
* The text to be displayed in the alert box.
*/
public function __construct($selector, $content) {
$this->selector = $selector;
$this->content = $content;

}

/**
* Implements Drupal\Core\Ajax\CommandInterface:render().
*/
public function render() {

return [
'command' => 'strawberryfield_codemirror',
'selector' => $this->selector,
'content' => $this->content,
];
}
}
37 changes: 37 additions & 0 deletions src/Controller/StrawberryfieldAdoRenderChildrenController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Drupal\strawberryfield\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Render\Renderer;
use Drupal\views\Views;

/**
* A Controller for rendering a view of an ADO's children.
*/
class StrawberryfieldAdoRenderChildrenController extends ControllerBase {

/**
* Renders the contestants page.
*
* @return array
* The renderable array.
*/
public function renderAdoChildrenViews() {
$view_names = ['ado_tools_children', 'ado_tools_children_creative_work_series'];
$return = [];
foreach ($view_names as $view_name) {
$view = Views::getView($view_name);
if(isset($view)) {
$view->execute();
$rendered = $view->render();
if(!empty($rendered['#rows'])) {
$output = \Drupal::service('renderer')->render($rendered);
$markup = ['#markup' => $output];
array_push($return, $markup);
}
}
}
return $return;
}
}
124 changes: 124 additions & 0 deletions src/Form/StrawberryfieldToolsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Drupal\strawberryfield\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\strawberryfield\Ajax\UpdateCodeMirrorCommand;

/**
* Returns responses for Node routes.
*/
class StrawberryfieldToolsForm extends FormBase {

public function getFormId() {
return 'strawberryfield_tools_form';
}

public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {

// For code Mirror
// @TODO make this module dependant
$settings['mode'] = 'application/ld+json';
$settings['readOnly'] = TRUE;
$settings['toolbar'] = FALSE;
$settings['lineNumbers'] = TRUE;

if ($sbf_fields = \Drupal::service('strawberryfield.utility')->bearsStrawberryfield($node)) {
foreach ($sbf_fields as $field_name) {
/* @var $field \Drupal\Core\Field\FieldItemInterface */
$field = $node->get($field_name);
if (!$field->isEmpty()) {
/** @var $field \Drupal\Core\Field\FieldItemList */
foreach ($field->getIterator() as $delta => $itemfield) {
// Note: we are not longer touching the metadata here.
/** @var $itemfield \Drupal\strawberryfield\Plugin\Field\FieldType\StrawberryFieldItem */
$json = json_encode(json_decode($itemfield->value), JSON_PRETTY_PRINT);
$form_state->set('itemfield', $itemfield);
$form['test_jmespath'] = [
'#type' => 'textfield',
'#default_value' => $form_state->getValue('test_jmespath'),
'#title' => $this->t('JMESPATH'),
'#description' => $this->t(
'Evaluate a JMESPath Query against this ADO\'s JSON. See <a href=":href" target="_blank">JMESPath Tutorial</a>.',
[':href' => 'http://jmespath.org/tutorial.html']
),

'#ajax' => [
'callback' => [$this, 'callJmesPathprocess'],
'event' => 'change',
'keypress' => FALSE,
'disable-refocus' => FALSE,
'progress' => [
// Graphic shown to indicate ajax. Options: 'throbber' (default), 'bar'.
'type' => 'throbber',
],
],
'#required' => TRUE,
'#executes_submit_callback' => TRUE,
'#submit' => ['::submitForm']
];
$form['test_output'] = [
aksm marked this conversation as resolved.
Show resolved Hide resolved
'#type' => 'codemirror',
'#prefix' => '<div id="jmespathoutput">',
'#suffix' => '</div>',
'#codemirror' => $settings,
'#default_value' => '{}',
'#rows' => 15,
'#attached' => [
'library' => [
'strawberryfield/jmespath_codemirror_strawberryfield',
],
],
];
$form['test_jmespath_input'] = [
'#type' => 'codemirror',
'#codemirror' => $settings,
'#default_value' => $json,
'#rows' => 15,
];
}
}
}
}
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Submit'),
'#attributes' => ['class' => ['js-hide']],
'#submit' => [[$this,'submitForm']]
];
return $form;
}

/**
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setRebuild();
}

/**
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return \Drupal\Core\Ajax\AjaxResponse
*/
public function callJmesPathprocess(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
/** @var $itemfield \Drupal\strawberryfield\Plugin\Field\FieldType\StrawberryFieldItem */
$itemfield = $form_state->get('itemfield');
try {
$result = $itemfield->searchPath($form_state->getValue('test_jmespath'));
}
catch (\Exception $exception) {
$result = $exception->getMessage();
}

$response->addCommand(new UpdateCodeMirrorCommand('#jmespathoutput', json_encode($result,JSON_PRETTY_PRINT)));

return $response;
}
}
47 changes: 47 additions & 0 deletions strawberryfield.install
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\views\Views;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;

/**
* Implements hook_install().
Expand Down Expand Up @@ -92,3 +96,46 @@ function strawberryfield_update_9102() {
}
}
}

/**
* Import views for ADO Tools children.
*
*/
function strawberryfield_update_9103() {
$view_names = ['ado_tools_children', 'ado_tools_children_creative_work_series'];
$views_set = true;
foreach ($view_names as $view_name) {
$view = Views::getView($view_name);
if(!isset($view)) {
$views_set = false;
}
}
if(!$views_set) {
strawberryfield_update_helper_install_configs();
}
}

/**
* Helper function to install all new configs.
*
* @param string $directory
* The configs directory.
*/
function strawberryfield_update_helper_install_configs($directory = InstallStorage::CONFIG_OPTIONAL_DIRECTORY) {
/** @var \Drupal\Core\Config\ConfigInstallerInterface $config_installer */
$config_installer = \Drupal::service('config.installer');
$config_installer->installDefaultConfig('module', 'strawberryfield');

$optional_install_path = \Drupal::moduleHandler()
->getModule('strawberryfield')
->getPath() . '/' . $directory;
if (is_dir($optional_install_path)) {
// Install any optional config the module provides.
$storage = new FileStorage($optional_install_path, StorageInterface::DEFAULT_COLLECTION);
$config_installer->installOptionalConfig($storage);
}
$restrict_by_dependency = [
'module' => 'strawberryfield',
];
$config_installer->installOptionalConfig(NULL, $restrict_by_dependency);
}
9 changes: 8 additions & 1 deletion strawberryfield.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ strawberryfield.d3viz:
- core/drupalSettings
- strawberryfield/d3js
- strawberryfield/d3jsplus
- core/drupal.form
- core/drupal.form
jmespath_codemirror_strawberryfield:
version: 1.0
js:
js/jmespath-codemirror_strawberryfield.js: {minified: false}
dependencies:
- core/jquery
- core/drupal
21 changes: 18 additions & 3 deletions strawberryfield.links.task.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Add an additional tab to the node page.
strawberryfield.custom_node_edit: # The ID of local task is the same as route, to make it easier to control.
route_name: 'strawberryfield.custom_node_edit'
title: 'Edit'
title: 'Edit'
base_route: 'entity.node.canonical'
weight: 3


strawberryfield_keynameprovider_collection:
title: 'JSON Key Name Providers'
route_name: entity.strawberry_keynameprovider.collection
Expand All @@ -16,4 +15,20 @@ strawberryfield_keynameprovider_overview_form:
route_name: 'strawberryfield.strawberryfield_keynameprovider_overview_form'
title: 'JSON Key Name Providers Overview'
base_route: 'entity.strawberry_keynameprovider.collection'
weight: 3
weight: 3

strawberryfield.ado_tools:
route_name: strawberryfield.ado_tools
base_route: entity.node.canonical
title: 'ADO Tools'
strawberryfield.ado_tools_jmespath:
route_name: strawberryfield.ado_tools
base_route: entity.node.canonical
title: 'JMESPATH'
parent_id: strawberryfield.ado_tools

strawberryfield.ado_tools_children:
route_name: strawberryfield.ado_tools_children
base_route: entity.node.canonical
title: 'ADO Children'
parent_id: strawberryfield.ado_tools
27 changes: 26 additions & 1 deletion strawberryfield.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,29 @@ strawberryfield.strawberryfield_keynameprovider_overview_form:
requirements:
_permission: 'access administration pages'
options:
_admin_route: TRUE
_admin_route: TRUE

strawberryfield.ado_tools:
path: '/node/{node}/adotools'
defaults:
_form: '\Drupal\strawberryfield\Form\StrawberryfieldToolsForm'
_title: 'ADO Tools'
requirements:
_entity_access: 'node.update'
options:
_node_operation_route: TRUE
parameters:
node:
type: 'entity:node'
strawberryfield.ado_tools_children:
path: '/node/{node}/adotools/children'
defaults:
_controller: '\Drupal\strawberryfield\Controller\StrawberryfieldAdoRenderChildrenController::renderAdoChildrenViews'
_title: 'Ado Children'
requirements:
_entity_access: 'node.update'
options:
_node_operation_route: TRUE
parameters:
node:
type: 'entity:node'