Skip to content

Commit

Permalink
added fieldmappting to block
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwirz committed Jan 5, 2021
1 parent 7490e2f commit a6c27e2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions iq_progressive_decoupler.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ initialize:
js:
//cdnjs.cloudflare.com/ajax/libs/twig.js/0.8.9/twig.min.js: { type: external, minified: true }
resources/js/twig-functions.js: {}
resources/js/fieldmapper.js: {}
resources/js/init.js: {}
dependencies:
- core/jquery
Expand Down
43 changes: 43 additions & 0 deletions resources/js/fieldmapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Field mapper class for rendering patterns.
*
* @constructor
* @param {Object} item - object containing the data to be rendered
* @param {Object} mapping - object containing the mapping
*/

iq_progessive_decoupler_FieldMapper = function (item, mapping) {
var self = this;
self.item = item;
self.mapping = mapping;

/**
* Apply mapping to item
*/
self.applyMappging = function (item = self.item, mapping = self.mapping) {
let output = {};
Object.keys(mapping).forEach(function(key){
output[key] = self.mapField(item, mapping[key]);
});
return output;
};


/**
* Apply mapping to item
*/
self.mapField = function (item, mapping) {
if (mapping.type == 'static') {
return mapping.value;
}
if (mapping.type == 'array') {
var arrayItem = eval(mapping.value);
arrayItem = arrayItem.map(function(item){
return self.applyMappging(item, mapping.mapping);
});
return arrayItem;
}
return eval(mapping.value);
};

}
16 changes: 16 additions & 0 deletions src/Plugin/Block/DecoupledBlockBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\ui_patterns\UiPatternsManager;
use Drupal\Component\Serialization\Yaml;
use Symfony\Component\Yaml\Yaml as YamlParser;
use Drupal\Component\Serialization\Yaml as YamlSerializer;

/**
* Base block for decoupling.
Expand Down Expand Up @@ -63,6 +66,13 @@ public function blockForm($form, FormStateInterface $form_state) {
'#default_value' => isset($this->configuration['ui_pattern']) ? $this->configuration['ui_pattern'] : NULL,
'#required' => TRUE,
];

$form['field_mapping'] = [
'#type' => 'textarea',
'#title' => $this->t('Field mapping'),
'#default_value' => isset($this->configuration['field_mapping']) ? Yaml::decode($this->configuration['field_mapping']) : NULL,
];

return $form;
}

Expand All @@ -82,8 +92,13 @@ public function build() {
$build['#attached']['drupalSettings']['progressive_decoupler'][$this->configuration['block_id']] = [
'template' => \file_get_contents($pattern['base path'] . '/' . $pattern['template'] . '.html.twig'),
'ui_pattern' => $this->configuration['ui_pattern'],
'type' => $this->getPluginId(),
];

if (isset($this->configuration['field_mapping'])) {
$build['#attached']['drupalSettings']['progressive_decoupler'][$this->configuration['block_id']]['field_mapping'] = YamlParser::parse(YamlSerializer::decode($this->configuration['field_mapping']));
}

return $build;

}
Expand All @@ -95,6 +110,7 @@ public function blockSubmit($form, FormStateInterface $form_state) {
parent::blockSubmit($form, $form_state);
$this->configuration['ui_pattern'] = $form_state->getValue('ui_pattern');
$this->configuration['block_id'] = str_replace('_', '-', 'block-' . $form['id']['#default_value']);
$this->configuration['field_mapping'] = Yaml::encode($form_state->getValue('field_mapping'));
}

}

0 comments on commit a6c27e2

Please sign in to comment.