You are here

function panels_allowed_layouts::set_allowed in Panels 7.3

Same name and namespace in other branches
  1. 5.2 includes/common.inc \panels_allowed_layouts::set_allowed()
  2. 6.3 includes/common.inc \panels_allowed_layouts::set_allowed()
  3. 6.2 includes/common.inc \panels_allowed_layouts::set_allowed()

Manage panels_common_set_allowed_layouts(), the FAPI code for selecting allowed layouts.

MAKE SURE to set panels_allowed_layouts::allow_new before calling this method. If you want the panels API to handle saving these allowed layout settings, panels_allowed_layouts::module_name must also be set.

Below is a sample implementation; refer to the rest of the class documentation to understand all the specific pieces. Values that are intended to be replaced are wrapped with <>.

\n

function docdemo_allowed_layouts() {
  ctools_include('common', 'panels');
  if (!is_a($allowed_layouts = unserialize(variable_get('panels_common_allowed_layouts', serialize(''))), 'panels_allowed_layouts')) {
    $allowed_layouts = new panels_allowed_layouts();
    $allowed_layouts->allow_new = TRUE;
    $allowed_layouts->module_name = '<client_module_name>';
  }
  $result = $allowed_layouts
    ->set_allowed('<Desired client module form title>');
  if (in_array($allowed_layouts->form_state, array(
    'failed-validate',
    'render',
  ))) {
    return $result;
  }
  elseif ($allowed_layouts->form_state == 'submit') {
    drupal_goto('</path/to/desired/redirect>');
  }
}

\n

If $allowed_layouts->form_state == 'failed-validate' || 'render', then you'll need to return $result as it contains the structured form HTML generated by drupal_render_form() and is ready to be passed through index.php's call to theme('page', ...).

However, if $allowed_layouts->form_state == 'submit', then the form has been submitted and we should react. It's really up to your client module how you handle the rest; panels_allowed_layouts::save() (or panels_allowed_layouts::api_save(), if that's the route you're going) will have already been called, so if those methods handle your save routine, then all there is left to do is handle redirects, if you want. The current implementation of the allowed layouts form currently never redirects, so it's up to you to control where the user ends up next.

Parameters

string $title: Used to set the title of the allowed layouts form. If no value is given, defaults to 'Panels: Allowed Layouts'.

Return value

mixed $result

  • On the first passthrough when the form is being rendered, $result is the form's structured HTML, ready to be pushed to the screen with a call to theme('page', ...).
  • A successful second passthrough indicates a successful submit, and $result === panels_allowed_layouts::allowed_layout_settings. Returning it is simply for convenience.

File

includes/common.inc, line 147
Functions used by more than one panels client module.

Class

panels_allowed_layouts
Class definition for the allowed layouts governing structure.

Code

function set_allowed($title = 'Panels: Allowed Layouts') {
  $this
    ->sync_with_available();
  $form_id = 'panels_common_set_allowed_layouts';

  // TODO switch to drupal_build_form(); need to pass by ref.
  $form = drupal_retrieve_form($form_id, $this, $title);
  if ($result = drupal_process_form($form_id, $form)) {

    // Successful submit.
    $this->form_state = 'submit';
    return $result;
  }
  $this->form_state = isset($_POST['op']) ? 'failed-validate' : 'render';
  $result = drupal_render_form($form_id, $form);
  return $result;
}