You are here

public function PanelizerEntityDefault::hook_field_attach_form in Panelizer 7.3

Same name and namespace in other branches
  1. 7.2 plugins/entity/PanelizerEntityDefault.class.php \PanelizerEntityDefault::hook_field_attach_form()

File

plugins/entity/PanelizerEntityDefault.class.php, line 1897
Base class for the Panelizer Entity plugin.

Class

PanelizerEntityDefault
Base class for the Panelizer Entity plugin.

Code

public function hook_field_attach_form($entity, &$form, &$form_state, $langcode) {
  list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);

  // We'll store the form array here so that we can tell at the end if we
  // have any and need to add our fieldset.
  $widgets = array();

  // Need to track the number of actual visible widgets because
  // element_get_visible_children doesn't handle nested fields.
  $visible_widgets = 0;
  foreach ($this->plugin['view modes'] as $view_mode => $view_mode_info) {
    $view_bundle = $bundle . '.' . $view_mode;
    $panelizers = $this
      ->get_default_panelizer_objects($view_bundle);

    // Ignore view modes that don't have a choice, have no displays defined,
    // or already have their own custom display set up.
    if (!$this
      ->has_panel_choice($view_bundle) || empty($panelizers) || !empty($entity->panelizer[$view_mode]->did)) {
      continue;
    }
    $options = array();
    foreach ($panelizers as $name => $panelizer) {
      if (empty($panelizer->disabled)) {
        $options[$name] = $panelizer->title ? $panelizer->title : t('Default');
      }
    }

    // Load the configured default display.
    $default_value = $this
      ->get_default_display_name($bundle, $view_mode);

    // The selected value.
    $selected = $default_value;
    if (!empty($entity->panelizer[$view_mode]->name)) {
      $selected = $entity->panelizer[$view_mode]->name;
    }

    // Only display the selector if options were available.
    if (!empty($options)) {

      // Indicate which item is the default.
      if (isset($options[$default_value])) {
        $options[$default_value] .= ' (' . t("default for '@bundle'", array(
          '@bundle' => $bundle,
        )) . ')';
      }

      // If only one option is available, don't show the selector.
      if (count($options) === 1) {
        $widgets[$view_mode]['name'] = array(
          '#title' => $view_mode_info['label'],
          '#type' => 'value',
          '#value' => $selected,
          // Put these here because submit does not get a real entity with the
          // actual *(&)ing panelizer.
          '#revision_id' => isset($entity->panelizer[$view_mode]->revision_id) ? $entity->panelizer[$view_mode]->revision_id : NULL,
          '#entity_id' => isset($entity->panelizer[$view_mode]->entity_id) ? $entity->panelizer[$view_mode]->entity_id : NULL,
        );
      }
      else {
        $widgets[$view_mode]['name'] = array(
          '#title' => $view_mode_info['label'],
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => $selected,
          '#required' => TRUE,
          // Put these here because submit does not get a real entity with the
          // actual *(&)ing panelizer.
          '#revision_id' => isset($entity->panelizer[$view_mode]->revision_id) ? $entity->panelizer[$view_mode]->revision_id : NULL,
          '#entity_id' => isset($entity->panelizer[$view_mode]->entity_id) ? $entity->panelizer[$view_mode]->entity_id : NULL,
        );
        $visible_widgets++;
      }
    }
  }

  // Only display this if the entity has visible options available.
  if (!empty($widgets)) {
    $form_state['panelizer has choice'] = TRUE;
    $form['panelizer'] = array(
      '#type' => 'fieldset',
      '#access' => $this
        ->panelizer_access('choice', $entity, $view_mode),
      '#title' => t('Customize display'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#group' => 'additional_settings',
      '#attributes' => array(
        'class' => array(
          'panelizer-entity-options',
        ),
      ),
      '#attached' => array(
        'js' => array(
          ctools_attach_js('panelizer-vertical-tabs', 'panelizer'),
        ),
      ),
      '#weight' => -10,
      '#tree' => TRUE,
    ) + $widgets;

    // Optional fieldset description.
    if (!empty($this->plugin['bundles'][$bundle]['help'])) {
      $form['panelizer']['#description'] = $this->plugin['bundles'][$bundle]['help'];
    }

    // If there are no visible widgets, don't display the fieldset.
    if ($visible_widgets == 0) {
      $form['panelizer']['#access'] = FALSE;
    }
  }
}