You are here

function bean_form in Bean (for Drupal 7) 7

Bean form

4 string references to 'bean_form'
bean_add in includes/bean.pages.inc
Add bean page callback
bean_edit in includes/bean.pages.inc
Edit bean page callback
bean_services_create in ./bean.services.inc
Adds a new bean to a node and returns the bid.
bean_services_update in ./bean.services.inc
Updates a bean and returns the bid.

File

includes/bean.pages.inc, line 195
Bean Functions

Code

function bean_form($form, &$form_state, Bean $bean, $type = NULL) {

  // During initial form build, add the bean entity to the form state for use
  // during form building and processing. During a rebuild, use what is in the
  // form state.
  if (!isset($form_state['bean'])) {
    if (!isset($bean->label)) {
      $bean->label = NULL;
    }
    if (isset($type)) {
      $bean->type = $type;
    }
    $form_state['bean'] = $bean;
  }
  else {
    $bean = $form_state['bean'];
  }

  // Add entity api defaults
  $form['#entity_type'] = 'bean';
  $form['bean']['#value'] = $bean;
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#required' => TRUE,
    '#default_value' => $bean->label,
    '#description' => t('Name that displays in the admin interface'),
    '#weight' => -10,
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#description' => t('The Title of the block. The <em>, <strong>, <i> and <b> HTML tags are allowed, all others will be filtered out.'),
    '#default_value' => $bean->title,
    '#weight' => -9,
  );
  $form['revision'] = array(
    '#weight' => 10,
  );
  if (isset($bean->is_new)) {
    $form['revision']['#access'] = FALSE;
  }
  ctools_include('dependent');
  ctools_add_js('dependent');
  $form['revision']['is_new_revision'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create new revision'),
    '#default_value' => 1,
    '#id' => 'edit-revision',
  );
  if (isset($bean->is_new)) {
    $form['default_revision'] = array(
      '#type' => 'value',
      '#value' => TRUE,
    );
  }
  else {
    $form['revision']['default_revision'] = array(
      '#type' => 'checkbox',
      '#title' => t('Set Revision as default'),
      '#default_value' => $bean
        ->isDefaultRevision(),
      '#states' => array(
        // Hide if the option above is disabled, to avoid potential dataloss.
        'invisible' => array(
          ':input[name="is_new_revision"]' => array(
            'checked' => FALSE,
          ),
        ),
      ),
    );
  }
  $form['revision']['log'] = array(
    '#type' => 'textarea',
    '#title' => t('Log message'),
    '#description' => t('Provide an explanation of the changes you are making. This will help other authors understand your motivations.'),
    '#dependency' => array(
      'edit-revision' => array(
        1,
      ),
    ),
    '#default_value' => '',
  );

  // The view mode.
  if (user_access('edit bean view mode')) {
    $bean_info = $bean
      ->entityInfo();
    foreach ($bean_info['view modes'] as $view_mode_name => $data) {
      $view_modes[$view_mode_name] = $data['label'];
    }
    $form['view_mode'] = array(
      '#title' => t('View Mode'),
      '#description' => t('Edit the view mode of the Bean'),
      '#default_value' => $bean->view_mode,
      '#type' => 'select',
      '#required' => TRUE,
      '#options' => $view_modes,
      '#weight' => -8,
    );
  }
  else {
    $form['view_mode'] = array(
      '#type' => 'value',
      '#value' => $bean->view_mode,
    );
  }
  $form['bean'] = array(
    '#type' => 'value',
    '#value' => $bean,
  );

  // Get the Bean type form.
  $form += $bean
    ->getForm($form, $form_state);

  // Add the buttons.
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 0,
    '#submit' => array(
      'bean_form_submit',
    ),
  );
  if (!empty($bean->bid) && entity_access('delete', 'bean', $bean)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#weight' => 1,
      '#submit' => array(
        'bean_form_delete_submit',
      ),
    );
  }
  $form['#validate'][] = 'bean_form_validate';
  if (!isset($form['#submit']) && function_exists($bean->type . '_bean_form_submit')) {
    $form['#submit'][] = $bean->type . '_bean_form_submit';
  }
  $form += array(
    '#submit' => array(),
  );
  if (function_exists('entity_language')) {
    $langcode = entity_language('bean', $bean);
  }
  else {
    $langcode = NULL;
  }
  field_attach_form('bean', $bean, $form, $form_state, $langcode);
  return $form;
}