diff --git a/includes/bean.inline_entity_form.inc b/includes/bean.inline_entity_form.inc index 4bae882..8d9fb48 100644 --- a/includes/bean.inline_entity_form.inc +++ b/includes/bean.inline_entity_form.inc @@ -34,6 +34,40 @@ class BeanInlineEntityFormController extends EntityInlineEntityFormController { } /** + * Overrides EntityInlineEntityFormController::defaultSettings(). + */ + public function defaultSettings() { + $defaults = parent::defaultSettings(); + $defaults['parent_sync_label'] = FALSE; + $defaults['parent_sync_title'] = FALSE; + + return $defaults; + } + + /** + * Overrides EntityInlineEntityFormController::settingsForm(). + */ + public function settingsForm($field, $instance) { + $form = parent::settingsForm($field, $instance); + + $form['parent_sync_label'] = array( + '#type' => 'checkbox', + '#title' => t('Auto set the block label to the parent @entity entity label', array('@entity' => $instance['entity_type'])), + '#description' => t('This will hide the label input field and set the label to the @entity entity label.', array('@entity' => $instance['entity_type'])), + '#default_value' => $this->settings['parent_sync_label'], + ); + + $form['parent_sync_title'] = array( + '#type' => 'checkbox', + '#title' => t('Auto set the block title to the parent @entity entity label', array('@entity' => $instance['entity_type'])), + '#description' => t('This will hide the title input field and set the title to the @entity entity label.', array('@entity' => $instance['entity_type'])), + '#default_value' => $this->settings['parent_sync_title'], + ); + + return $form; + } + + /** * Overrides EntityInlineEntityFormController::entityForm(). */ public function entityForm($entity_form, &$form_state) { @@ -55,6 +89,15 @@ class BeanInlineEntityFormController extends EntityInlineEntityFormController { '#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, ); @@ -128,6 +171,30 @@ class BeanInlineEntityFormController extends EntityInlineEntityFormController { // 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); } @@ -149,8 +216,23 @@ class BeanInlineEntityFormController extends EntityInlineEntityFormController { $ief_form_state['values'] = $ief_form_state['values'][$parent]; } + // Get lables to set temporary label and title. + $display_labels = $this->labels(); + + // Set label since it will be auto generated based on the parent entity. + if (empty($ief_form_state['bean']->label) && $this->settings['parent_sync_label']) { + $ief_form_state['values']['label'] = $display_labels['singular']; + } + + // Set title since it will be auto generated based on the parent entity. + if (empty($ief_form_state['bean']->title) && $this->settings['parent_sync_title']) { + $ief_form_state['values']['title'] = $display_labels['singular']; + } + // Load bean.pages.inc so we can call bean_form_submit module_load_include('inc', 'bean', 'includes/bean.pages'); + + // Saves initial bean via bean_form_submit(). bean_form_submit($entity_form, $ief_form_state); // Undo the redirect set by bean_form_submit(). @@ -168,6 +250,20 @@ class BeanInlineEntityFormController extends EntityInlineEntityFormController { * Overrides EntityInlineEntityFormController::save(). */ public function save($entity, $context) { + if ($this->settings['parent_sync_label'] || $this->settings['parent_sync_title']) { + $parent_label = entity_label($context['parent_entity_type'], $context['parent_entity']); + + // Set label to the parent label. + if ($this->settings['parent_sync_label']) { + $entity->label = $parent_label; + } + + // Set title to the parent label. + if ($this->settings['parent_sync_title']) { + $entity->title = $parent_label; + } + } + bean_save($entity); } }