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 2 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
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;
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,
];
}
}
157 changes: 157 additions & 0 deletions src/Form/StrawberryfieldToolsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace Drupal\strawberryfield\Form;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\strawberryfield\Ajax\UpdateCodeMirrorCommand;

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

/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;

/**
* Constructs a StrawberryfieldToolsForm object.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(RendererInterface $renderer, EntityRepositoryInterface $entity_repository) {
$this->entityRepository = $entity_repository;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('renderer'),
$container->get('entity.repository')
);
}

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_jmespath_input'] = [
'#type' => 'codemirror',
'#codemirror' => $settings,
'#default_value' => $json,
'#rows' => 15,
];
$form['test_output'] = [
'#type' => 'codemirror',
'#prefix' => '<div id="jmespathoutput">',
'#suffix' => '</div>',
'#codemirror' => $settings,
'#default_value' => '{}',
'#rows' => 15,
'#attached' => [
'library' => [
'strawberryfield/jmespath_codemirror_strawberryfield',
],
],
];
}
}
}
}
$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;
}
}
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
10 changes: 7 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,9 @@ 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'
15 changes: 14 additions & 1 deletion strawberryfield.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,17 @@ 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'