evocms-community / pagebuilder

Page Builder for Evolution CMS
29 stars 23 forks source link

How to use a "global placeholder" (field) from a "wrapping" field in nested group field? #72

Closed Aharito closed 6 years ago

Aharito commented 6 years ago

How to use a "global placeholder" (field) from a "wrapping" field in nested group field? Example:

'templates' => [
  'owner' => '[+images+]',

 //crop_global must be "global" crop values for all images and be set once
  'images' => '<img src="[[phpthumb? &input=`[+image+]` &options=`[+crop_global+]`]]">',
],

'fields' => [
  //crop_global
   'crop_global' => [
       'caption'  => 'Обрезка картинок',
       'type'     => 'dropdown',
       'elements' => 'w=1074||w=537||w=268',          
   ],  

   'images' => [
      'caption' => 'Картинки',
      'type'    => 'group',
      'fields' => [
         'image' => [
             'caption' => 'Картинка',
             'type'    => 'image',
         ],            
      ],
   ],
],

I would like to set the crop_global field once, not to set it separately for each image. So that the crop_global value would be used to crop any new image in the nested "images" group field.

But inside the nested template, the "external" placeholder is not available. Are there any solutions?

mnoskov commented 6 years ago

Hm, maybe it can be solved by prepare function Example (not tested):

'prepare' => function($options, $values) {
    foreach ($values['images'] as &$image) {
        $image['crop'] = $values['crop_global'];
    }
    unset($image);
}
Aharito commented 6 years ago

Based on the code of your pagebuilder.php file, should the "prepare" function be inserted into an options array of the corresponding field? and what field is corresponding? "images"?

Following example not work:

   'images' => [
      'caption' => 'Картинки',
      'type'    => 'group',
      'prepare' => function($options, $values) {
          foreach ($values['images'] as &$image) {
              $image['crop'] = $values['crop_global'];
          }
          unset($image);
      },
      'fields' => [
         'image' => [
             'caption' => 'Картинка',
             'type'    => 'image',
         ],            
      ],
mnoskov commented 6 years ago
'templates' => [
  'owner' => '[+images+]',

 //crop_global must be "global" crop values for all images and be set once
  'images' => '<img src="[[phpthumb? &input=`[+image+]` &options=`[+crop_global+]`]]">',
],

'fields' => [
  //crop_global
   'crop_global' => [
       'caption'  => 'Обрезка картинок',
       'type'     => 'dropdown',
       'elements' => 'w=1074||w=537||w=268',          
   ],  

   'images' => [
      'caption' => 'Картинки',
      'type'    => 'group',
      'fields' => [
         'image' => [
             'caption' => 'Картинка',
             'type'    => 'image',
         ],            
      ],
   ],
],

'prepare' => function($options, $values) {
    foreach ($values['images'] as &$image) {
        $image['crop'] = $values['crop_global'];
    }
    unset($image);
}
Aharito commented 6 years ago

Unfortunately, not work. I tried using [+crop+] instead of [+crop_global+] in 'images' template, but it still does not work.

mnoskov commented 6 years ago

Try

'prepare' => function($options, &$values) {

instead of

'prepare' => function($options, $values) {
Aharito commented 6 years ago

Thanks, 'prepare' => function($options, &$values) variant works well with [+crop+] placeholder.