Skip to content

Instantly share code, notes, and snippets.

@jec006
Created July 11, 2012 19:04
Show Gist options
  • Save jec006/3092403 to your computer and use it in GitHub Desktop.
Save jec006/3092403 to your computer and use it in GitHub Desktop.
<?php
/**
* Implements ConditionInterface::getElement().
*
* Gets the form for the condition.
*
* @see sps_condition_form_validate_callback
* @see sps_condition_form_submit_callback
*/
public function getElement($element, &$form_state) {
$sub_state = $form_state;
$sub_state['values'] = isset($form_state['values']['widget']) ? $form_state['values']['widget'] : array();
$element['widget'] = $this->widget->getPreviewForm(array(), $sub_state);
$element['widget']['#tree'] = TRUE;
$element['preview'] = array(
'#type' => 'submit',
'#value' => 'Preview',
);
$element['#sps_validate'] = array($this, 'validateElement');
$element['#validate'] = 'sps_condition_form_validate_callback';
$element['#sps_submit'] = array($this, 'submitElement');
$element['#submit'] = 'sps_condition_form_submit_callback';
return $element;
}
<?php
/**
* A validate callback for forms which will subsequently call the forms
* #sps_validate function.
*
* This allows us to call class methods as form validators. To use #sps_validate,
* should be set to a callable item, and #submit set to this function.
*/
function sps_condition_form_validate_callback($form, &$form_state) {
if (!empty($form['#sps_validate']) && is_callable($form['#sps_validate'])) {
$form['#sps_validate']($form, $form_state);
}
}
/**
* A submit callback for forms which will subsequently call the forms
* #sps_submit function.
*
* This allows us to call class methods as form submitters. To use #sps_submit,
* should be set to a callable item, and #submit set to this function.
*/
function sps_condition_form_submit_callback($form, &$form_state) {
if (!empty($form['#sps_submit']) && is_callable($form['#sps_submit'])) {
$form['#sps_submit']($form, $form_state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment