Filter on Preview Page Url

7 posts by 3 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: May 7, 2013   (RSS)

By gregThomas - April 26, 2013

Hi Djulia,

Could you give me a bit more detail on what you're trying to achieve? 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Djulia - April 26, 2013

Hi Greg,

I would like to obtain a URL different according to the value from another field for _previewUrl on action=edit.

//
function _previewUrl_modHidden($html, $tablename, $record) {
  if ($record['field'] == 1) { $previewUrl = '<input type="hidden" name="_previewUrl" id="previewUrl" value="file_one.php?preview-xxxx" />' }
  else { $previewUrl = '<input type="hidden" name="_previewUrl" id="previewUrl" value="file_two.php?preview-xxxx" />'  }  
}

Do you think that it is possible?

Thanks again!

Djulia

By gregThomas - April 30, 2013

Hi Djulia, 

This can be done, but you'll have to edit core files in CMSB to make it work. If you go to cmsAdmin/lib/menus/default/edit.php on line 31 there should be code that creates the preview URL, to create a second preview you need to modify it to look like this:

  $previewUrl = @$schema['_previewPage'] ? PREFIX_URL.@$schema['_previewPage'] : PREFIX_URL.@$schema['_detailPage'];

  if($schema['_tableName'] == 'blog' && $GLOBALS['RECORD']['hidden']){
     $previewUrl = PREFIX_URL.'/test.php';
  }

  if ($previewUrl) { $previewUrl .= '?' .urlencode(t('preview')). '-9999999999'; } // note that 9999999999 is a special number which getRecords() uses to know this is a preview request
  if (@$schema['_disablePreview']) { $previewUrl = ''; }

This is just example code, so you'll need to make a few changes to get it working.

You'll need to change 'blog' to the section name that you want a switchable preview button for, the $GLOBALS['RECORD']['hidden'] variable should be the name of the field that decides which preview page the system goes to (in this example I've used the hidden checkbox field). Finally, you need to change PREFIX_URL.'/test.php' to the second preview page.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Dave - May 2, 2013

Hi Djulia,

And one more approach for you, here the relevant code from /lib/menus/default/edit.php

  //
  doAction('record_preedit', $tableName, @$_REQUEST['num']);

  //
  $previewUrl = @$schema['_previewPage'] ? PREFIX_URL.@$schema['_previewPage'] : PREFIX_URL.@$schema['_detailPage'];
  if ($previewUrl) { $previewUrl .= '?' .urlencode(t('preview')). '-9999999999'; } // note that 9999999999 is a special number which getRecords() uses to know this is a preview request
  if (@$schema['_disablePreview']) { $previewUrl = ''; }

You could write a plugin that is called on 'record_preedit' that modifies $GLOBALS['schema']['_previewPage'].  Would that work for you?

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By Dave - May 2, 2013

... that would allow you to modify the preview url based on the value of a record that is already saved.  

If you wanted to modify the preview url dynamically as the user entered different content you could do that with jQuery by adding some code to the footer on the 'admin_footer' plugin action.

Dave Edis - Senior Developer
interactivetools.com

By Djulia - May 7, 2013 - edited: May 7, 2013

My apologies for the delay.

@Dave - Your approach is perfect for me. Thanks!

addAction('record_preedit', '_custom_previewUrl_addPluginAction');

function _custom_previewUrl_addPluginAction() {
    global $schema;
    if ($schema['_tableName'] == 'my_table'
            $schema['_previewPage'] = '../';
    }
}

@Greg - Thanks for your patience!

Djulia