You are here

public function BeanInlineEntityFormController::entityForm in Bean (for Drupal 7) 7

Overrides EntityInlineEntityFormController::entityForm().

Overrides EntityInlineEntityFormController::entityForm

File

includes/bean.inline_entity_form.inc, line 73
Defines the inline entity form controller for Nodes.

Class

BeanInlineEntityFormController
@file Defines the inline entity form controller for Nodes.

Code

public function entityForm($entity_form, &$form_state) {
  $bean = $entity_form['#entity'];
  $type = bean_type_load($bean->type);
  $extra_fields = field_info_extra_fields('bean', $bean->type, 'form');
  if (!isset($bean->label)) {
    $bean->label = NULL;
  }
  $entity_form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('@type Label', array(
      '@type' => check_plain($type
        ->getLabel()),
    )),
    '#required' => TRUE,
    '#default_value' => $bean->label,
    '#maxlength' => 255,
    // Check if extra fields define a weight.
    '#weight' => !empty($extra_fields['label']) ? $extra_fields['label']['weight'] : -5,
  );
  $entity_form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('@type Title', array(
      '@type' => check_plain($type
        ->getLabel()),
    )),
    '#default_value' => $bean->title,
    '#maxlength' => 255,
    // Check if extra fields define a weight.
    '#weight' => !empty($extra_fields['title']) ? $extra_fields['title']['weight'] : -5,
  );
  $entity_form['revision'] = array(
    '#weight' => 10,
  );
  if (isset($bean->is_new)) {
    $entity_form['revision']['#access'] = FALSE;
  }
  ctools_include('dependent');
  ctools_add_js('dependent');
  $entity_form['revision']['is_new_revision'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create new revision'),
    '#default_value' => 1,
    '#id' => 'edit-revision',
  );
  if (isset($bean->is_new)) {
    $entity_form['default_revision'] = array(
      '#type' => 'value',
      '#value' => TRUE,
    );
  }
  else {
    $entity_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,
          ),
        ),
      ),
    );
  }
  $entity_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'];
    }
    $entity_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,
      // Check if extra fields define a weight.
      '#weight' => !empty($extra_fields['view_mode']) ? $extra_fields['view_mode']['weight'] : -8,
    );
  }
  else {
    $entity_form['view_mode'] = array(
      '#type' => 'value',
      '#value' => $bean->view_mode,
    );
  }

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

  // Hide the title field if it is auto-generated.
  if ($this->settings['parent_sync_label']) {
    $entity_form['label']['#required'] = FALSE;
    $entity_form['label']['#access'] = FALSE;
  }

  // Hide the title field if it is auto-generated.
  if ($this->settings['parent_sync_title']) {
    $entity_form['title']['#required'] = FALSE;
    $entity_form['title']['#access'] = FALSE;

    // Hide the replacement field added by the Title module as well.
    if (module_exists('title')) {
      $title_field = title_field_replacement_info('bean', 'title');
      if ($title_field) {
        $title_field_name = $title_field['field']['field_name'];
        if (isset($entity_form[$title_field_name])) {
          $entity_form[$title_field_name]['#access'] = FALSE;
          $entity_form[$title_field_name]['#required'] = FALSE;
        }
      }
    }
  }
  return parent::entityForm($entity_form, $form_state);
}