Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save IT-Cru/e5e8df1d99d1229ad1b0a9063e91c679 to your computer and use it in GitHub Desktop.
Save IT-Cru/e5e8df1d99d1229ad1b0a9063e91c679 to your computer and use it in GitHub Desktop.
drupal/imagepin: patches for BurdaStyle projects
diff --git a/css/imagepin.admin.css b/css/imagepin.admin.css
index e92e5c7..fada652 100755
--- a/css/imagepin.admin.css
+++ b/css/imagepin.admin.css
@@ -7,16 +7,23 @@ table .imagepin {
position: static !important;
}
-.imagepin-widget-element-form-table tbody tr td:nth-child(1) {
+.imagepin-widget-element-form-table tbody tr td:nth-child(1),
+.imagepin-edit-table-head-1 {
width: 5%;
}
.imagepin-widget-element-form-table tbody tr td:nth-child(2) {
- width: 70%;
+ width: 65%;
}
-.imagepin-widget-element-form-table tbody tr td:nth-child(3) {
- width: 25%;
+.js-imagepin-edit-table-head-2, .js-imagepin-edit-table-head-3 {
+ text-align: center;
+ width: 32.5%;
+}
+
+.imagepin-widget-element-form-table tbody tr td:nth-child(3),.imagepin-widget-element-form-table tbody tr td:nth-child(4),
+.imagepin-edit-table-head-4 {
+ width: 30%;
}
.imagepin-draggable {
@@ -26,3 +33,8 @@ table .imagepin {
.imagepin-positions {
padding-top: 15px;
}
+
+input.imagepin-widget-input{
+ float: left;
+ width: 49%;
+}
\ No newline at end of file
diff --git a/imagepin.attach.inc b/imagepin.attach.inc
index f76fe9b..8371041 100644
--- a/imagepin.attach.inc
+++ b/imagepin.attach.inc
@@ -59,6 +59,26 @@ function imagepin_attach(array &$element, array &$items) {
* @see imagepin_attach().
*/
function imagepin_add_widgets_element(array $element) {
+
+ $media = $element['#item']->getParent()->getParent()->getValue();
+
+ if($media->_referringItem != Null){
+ $renderer = \Drupal::service('renderer');
+ $parent = $media->_referringItem->getEntity();
+ $high = $parent->get('field_look_label')->getValue()[0]['value'];
+ $low = $parent->get('field_look_label')->getValue()[1]['value'];
+
+ $renderer->addCacheableDependency($element, $parent);
+
+ if(isset($parent->_referringItem)) {
+ $renderer->addCacheableDependency($element, $parent->_referringItem->getEntity());
+ }
+
+ }else{
+ $high = '';
+ $low = '';
+ }
+
if (!empty($element['#item']) && !empty($element['#imagepin'])) {
$image_fid = $element['#item']->get('target_id')->getValue();
if (!empty($element['#imagepin']['widgets'][$image_fid])) {
@@ -66,6 +86,8 @@ function imagepin_add_widgets_element(array $element) {
$element['#item_attributes']['data-imagepin-attach-from'] = $attach_id;
$element['#imagepin_widgets'] = [
'#theme' => 'imagepin_widgets',
+ '#high' => $high,
+ '#low' => $low,
'#widgets' => $element['#imagepin']['widgets'][$image_fid],
'#attach_id' => $attach_id,
'#breakpoint' => $element['#imagepin']['settings']['breakpoint'],
@@ -83,17 +105,5 @@ function imagepin_add_widgets_element(array $element) {
*/
function imagepin_add_widgets_markup($markup, $element) {
$widgets_html = render($element['#imagepin_widgets']);
-
- $media = $element['#item']->getParent()->getParent()->getValue();
-
- $element = [
- '#theme' => 'imagepin_pins',
- '#content' => $markup,
- '#copyright' => $media->get('field_copyright')->getValue()[0]['value'],
- '#description' => $media->get('field_description')->getValue()[0]['value'],
- '#source' => $media->get('field_source')->getValue()[0]['value'],
- '#pins' => $widgets_html
- ];
-
- return render($element);
-}
\ No newline at end of file
+ return $markup . $widgets_html;
+}
diff --git a/imagepin.module b/imagepin.module
index 6a16fa9..a5dbe40 100755
--- a/imagepin.module
+++ b/imagepin.module
@@ -36,6 +36,8 @@ function imagepin_theme($existing, $type, $theme, $path) {
'widgets' => [],
'attach_id' => '',
'breakpoint' => '1024',
+ 'high' => '',
+ 'low' => '',
],
'file' => 'imagepin.theme.inc',
],
diff --git a/js/imagepin.edit.js b/js/imagepin.edit.js
index f010e55..262921b 100755
--- a/js/imagepin.edit.js
+++ b/js/imagepin.edit.js
@@ -133,8 +133,18 @@
$(".imagepin[data-imagepin-key='" + key + "']").remove();
});
+ /* Setting headlines for Imagepin edit table cols dynamically, because labels
+ are not persistant at the moment of creation */
+ if($('.js-imagepin-edit-table-head-2', context).length > 0) {
+ $('.js-imagepin-edit-table-head-2').text($('#edit-field-look-label-0-value').val());
+ };
+ if($('.js-imagepin-edit-table-head-3', context).length > 0) {
+ $('.js-imagepin-edit-table-head-3').text($('#edit-field-look-label-1-value').val());
+ };
+
},
detach: function (context, settings) {}
};
+
}(jQuery, Drupal, window));
diff --git a/src/Form/PinWidgetsForm.php b/src/Form/PinWidgetsForm.php
index 969647a..9a71dcd 100755
--- a/src/Form/PinWidgetsForm.php
+++ b/src/Form/PinWidgetsForm.php
@@ -13,6 +13,8 @@ use Drupal\imagepin\Plugin\WidgetManager;
use Drupal\imagepin\WidgetRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
+use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
+use Drupal\advertising_products\Plugin\Field\FieldWidget;
/**
* Delivers a form for pinning widgets on an image.
@@ -255,10 +257,12 @@ class PinWidgetsForm extends FormBase {
];
$this->buildAvailableWidgetElements($form, $form_state, $view_mode);
+ if($dinge == true){
+
$form['display'][$view_mode]['widgets']['new'] = [
'#type' => 'fieldset',
- '#title' => $this->t('New'),
+ '#title' => $this->t('New'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'add' => [
@@ -325,7 +329,7 @@ class PinWidgetsForm extends FormBase {
}
$this->switchWidgetElement($form, $form_state, $default_plugin, $view_mode);
}
-
+ }
// Move multiple editable view modes into vertical tabs.
if (count($view_modes_pinable) > 1) {
$form['display_tabs'] = [
@@ -381,6 +385,49 @@ class PinWidgetsForm extends FormBase {
return $form['display'][$view_mode]['widgets']['new']['widget'];
}
+ /**
+ * Submit handler for adding a new widget.
+ *
+ * @param array &$form
+ * The form array.
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
+ * The form state.
+ * @param \Symfony\Component\HttpFoundation\Request $request
+ * (Optional) The current request object, usually delivered by the AJAX API.
+ */
+ public function addNewWidget(array &$form, FormStateInterface $form_state, Request $request = NULL) {
+ $trigger = $form_state->getTriggeringElement();
+ $view_mode = $trigger['#parents'][1];
+ $plugin_id = 'product';
+ $widget = $this->widgetManager->createInstance($plugin_id);
+
+ $display = $form_state->getValue('display');
+ $value = $display[$view_mode]['widgets']['available']['elements']['new']['elements'][0]['preview']['element'];
+ $belonging = ['view_mode' => $view_mode] + $form_state->getValue('belonging');
+ unset($form['display'][$view_mode]['widgets']['available']['elements']['new']);
+
+ $this->widgetRepository->save($widget, $value, $belonging);
+
+
+ if ($request instanceof Request) {
+
+ $userInput = $form_state->getUserInput();
+ $keys = $form_state->getCleanValueKeys();
+ $newInputArray = [];
+
+ foreach ($keys as $key) {
+ if ($key == "op") continue;
+ $newInputArray[$key] = $userInput[$key];
+ }
+
+ $form_state->setUserInput($newInputArray);
+ $form_state->setRebuild(true);
+ $form = $this->formBuilder->rebuildForm($this->getFormId(), $form_state, $form);
+ }
+
+ return $form['display'][$view_mode]['widgets']['available'];
+ }
+
/**
* Submit handler for adding a new widget.
*
@@ -391,23 +438,32 @@ class PinWidgetsForm extends FormBase {
* @param \Symfony\Component\HttpFoundation\Request $request
* (Optional) The current request object, usually delivered by the AJAX API.
*/
- public function addNewWidget(array &$form, FormStateInterface $form_state, Request $request = NULL) {
+ public function updateWidget(array &$form, FormStateInterface $form_state, Request $request = NULL) {
$trigger = $form_state->getTriggeringElement();
$view_mode = $trigger['#parents'][1];
- $plugin_id = $form_state->getValue('display')[$view_mode]['widgets']['new']['plugin'];
+ $widget_key = $trigger['#parents'][5];
+
+ $plugin_id = 'product';
$widget = $this->widgetManager->createInstance($plugin_id);
$display = $form_state->getValue('display');
- $value = $display[$view_mode]['widgets']['new']['widget'][$plugin_id];
+ $value = $display[$view_mode]['widgets']['available']['elements'][$widget_key]['elements'][0]['preview']['element'];
$belonging = ['view_mode' => $view_mode] + $form_state->getValue('belonging');
- $this->widgetRepository->save($widget, $value, $belonging);
+ $positions = $form_state->getValue(['display', $view_mode, 'image', 'positions', 'settings']);
+ $positions = $this->jsonSerializer->decode($positions);
- if ($request instanceof Request) {
- $form_state->setRebuild();
- $form = $this->formBuilder->rebuildForm($this->getFormId(), $form_state, $form);
- }
+ $widgets = $this->widgetRepository->loadBelonging($belonging);
+
+ $value['position'] = $widgets[$widget_key]['value']['position'];
+
+ $this->widgetRepository->save($widget, $value, $belonging, $widget_key);
- return $form['display'][$view_mode]['widgets'];
+ if ($request) {
+ $form_state->setRebuild();
+ $form = $this->formBuilder->rebuildForm($this->getFormId(), $form_state, $form);
+ }
+
+ return $form['display'][$view_mode]['widgets']['available'];
}
/**
@@ -433,6 +489,31 @@ class PinWidgetsForm extends FormBase {
'#attributes' => ['id' => 'available-' . $view_mode],
];
$elements = [];
+
+ $elements['imagepin-edit-table'] = [
+ '#type' => 'table',
+ '#attributes' => ['class' => ['imagepin-edit-table']],
+ //'#header' => array($this->t('Name'), $this->t('Phone')),
+ '#header' => [
+ [
+ 'class' => ['imagepin-edit-table-head imagepin-edit-table-head-1'],
+ 'data' => $this->t('Pin'),
+ ],
+ [
+ 'class' => ['imagepin-edit-table-head js-imagepin-edit-table-head-2'],
+ 'data' => $this->t('Headline'),
+ ],
+ [
+ 'class' => ['imagepin-edit-table-head js-imagepin-edit-table-head-3'],
+ 'data' => $this->t('Headline2'),
+ ],
+ [
+ 'class' => ['imagepin-edit-table-head imagepin-edit-table-head-4'],
+ 'data' => $this->t('Actions'),
+ ],
+ ],
+ ];
+
foreach ($available as $key => $widget) {
$pin = [
'#type' => 'container',
@@ -444,10 +525,12 @@ class PinWidgetsForm extends FormBase {
$this->jsonSerializer->encode($position);
}
+ $product_one = \Drupal::entityTypeManager()->getStorage('advertising_product')->load($widget['value']['product']);
+ $product_two = \Drupal::entityTypeManager()->getStorage('advertising_product')->load($widget['value']['product2']);
+
$elements[$key] = [
'#type' => 'container',
'#attributes' => ['id' => 'available-widget-' . $key, 'class' => ['container-inline']],
- '#weight' => $key * 10,
'elements' => [
'#type' => 'table',
'#attributes' => ['class' => 'imagepin-widget-element-form-table'],
@@ -461,11 +544,26 @@ class PinWidgetsForm extends FormBase {
'element' => [
'#type' => 'container',
'#attributes' => ['class' => ['imagepin-widget']],
- 'preview' => [
- '#type' => 'container',
- '#attributes' => ['class' => ['imagepin-widget-preview']],
- 'widget' => $widget['plugin']->previewContent($widget['value']),
- ],
+ 'product' => [
+ '#type' => 'entity_autocomplete',
+ '#target_type' => 'advertising_product',
+ '#selection_handler' => 'advertising_products:product',
+ '#required' => FALSE,
+ '#default_value' => $product_one,
+ '#attributes' => ['class' => ['imagepin-widget-input']],
+ '#maxlength' => 1024,
+
+ ],
+ 'product2' => [
+ '#type' => 'entity_autocomplete',
+ '#target_type' => 'advertising_product',
+ '#selection_handler' => 'advertising_products:product',
+ '#required' => FALSE,
+ '#default_value' => $product_two,
+ '#attributes' => ['class' => ['imagepin-widget-input']],
+ '#maxlength' => 1024,
+
+ ],
],
],
'actions' => [
@@ -486,15 +584,107 @@ class PinWidgetsForm extends FormBase {
],
],
],
+ 'update' => [
+ '#type' => 'submit',
+ '#name' => 'update_widget_' . $key,
+ '#value' => $this->t('Update'),
+ '#attributes' => ['class' => ['imagepin-update'], 'data-imagepin-key' => $key],
+ '#ajax' => [
+ 'callback' => [$this, 'updateWidget'],
+ 'wrapper' => 'available-' . $view_mode,
+ 'effect' => 'fade',
+ 'method' => 'replaceWith',
+ 'progress' => [
+ 'type' => 'throbber',
+ 'message' => '',
+ ],
+ ],
+ ],
],
],
],
];
}
- $form['display'][$view_mode]['widgets']['available']['elements'] = $elements;
+
+ $elements['new'] = [
+ '#type' => 'container',
+ '#prefix' => '<div id="modules-wrapper">',
+ '#suffix' => '</div>',
+ '#attributes' => ['id' => 'available-widget-new', 'class' => ['container-inline']],
+ 'elements' => [
+ '#type' => 'table',
+ '#attributes' => ['class' => 'imagepin-widget-element-form-table'],
+ 0 => [
+ 'pin' => [
+ '#weight' => 10,
+ 'element' => ['new'],
+ ],
+ 'preview' => [
+ '#weight' => 20,
+ 'element' => [
+ '#type' => 'container',
+ '#attributes' => ['class' => ['imagepin-widget']],
+ 'product' => [
+ '#type' => 'entity_autocomplete',
+ '#target_type' => 'advertising_product',
+ '#selection_handler' => 'advertising_products:product',
+ '#required' => FALSE,
+ '#attributes' => ['class' => ['imagepin-widget-input']],
+ '#maxlength' => 1024,
+ '#size' => 60,
+
+ ],
+ 'product2' => [
+ '#type' => 'entity_autocomplete',
+ '#target_type' => 'advertising_product',
+ '#selection_handler' => 'advertising_products:product',
+ '#required' => FALSE,
+ '#attributes' => ['class' => ['imagepin-widget-input']],
+ '#maxlength' => 1024,
+
+ ],
+ ],
+ ],
+ 'actions' => [
+ '#weight' => 100,
+ 'update' => [
+ '#type' => 'submit',
+ '#name' => 'update_widget_new',
+ '#value' => $this->t('Save'),
+ '#attributes' => ['class' => ['imagepin-update'], 'data-imagepin-key' => 'new'],
+ '#ajax' => [
+ 'callback' => [$this, 'addNewWidget'],
+ 'wrapper' => 'available-' . $view_mode,
+ 'effect' => 'fade',
+ 'method' => 'replaceWith',
+ 'progress' => [
+ 'type' => 'throbber',
+ 'message' => '',
+ ],
+ ],
+ ],
+ ],
+ ],
+ ],
+ ];
+
+
+ $max = $form_state->get('fields_count');
+ if(is_null($max)) {
+ $max = 0;
+ $form_state->set('fields_count', $max);
+ }
+
+
+
+
+
+ $form['display'][$view_mode]['widgets']['available']['elements'] = $elements;
return $form['display'][$view_mode]['widgets']['available']['elements'];
}
+
+
/**
* Submit handler for removing an existing widget.
*
diff --git a/imagepin.module b/imagepin.module
index 15aa594..eae5b6e 100755
--- a/imagepin.module
+++ b/imagepin.module
@@ -46,6 +46,7 @@ function imagepin_theme($existing, $type, $theme, $path) {
* Implements hook_library_info_build().
*/
function imagepin_library_info_build() {
+ return false;
$libraries = [];
$libraries['extensions'] =
['dependencies' => ['core/drupal', 'core/jquery'], 'css' => [], 'js' => []];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment