diff --git a/bean.info b/bean.info index 23ecde9..ec307dc 100644 --- a/bean.info +++ b/bean.info @@ -6,6 +6,7 @@ files[] = includes/bean.info.inc files[] = plugins/BeanPlugin.class.php files[] = plugins/BeanDefault.class.php files[] = includes/translation.handler.bean.inc +files[] = includes/bean.inline_entity_form.inc files[] = views/views_handler_filter_bean_type.inc files[] = views/views_handler_field_bean_type.inc files[] = views/views_handler_field_bean_edit_link.inc diff --git a/bean.module b/bean.module index c35ff1c..2913089 100644 --- a/bean.module +++ b/bean.module @@ -14,6 +14,10 @@ function bean_entity_info() { 'label' => t('Block'), 'entity class' => 'Bean', 'controller class' => 'BeanEntityAPIController', + // This class provides integration with the Inline entity form module. + 'inline entity form' => array( + 'controller' => 'BeanInlineEntityFormController', + ), 'base table' => 'bean', 'revision table' => 'bean_revision', 'fieldable' => TRUE, diff --git a/includes/bean.inline_entity_form.inc b/includes/bean.inline_entity_form.inc new file mode 100644 index 0000000..03d77e6 --- /dev/null +++ b/includes/bean.inline_entity_form.inc @@ -0,0 +1,162 @@ + t('block'), + 'plural' => t('blocks'), + ); + return $labels; + } + + /** + * Overrides EntityInlineEntityFormController::tableFields(). + */ + public function tableFields($bundles) { + $fields = array(); + + $fields['label'] = array( + 'type' => 'property', + 'label' => t('Label'), + 'weight' => 1, + ); + + $fields['title'] = array( + 'type' => 'property', + 'label' => t('Title'), + 'weight' => 2, + ); + + return $fields; + } + + /** + * Overrides EntityInlineEntityFormController::entityForm(). + */ + 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); + return parent::entityForm($entity_form, $form_state); + } + + /** + * Overrides EntityInlineEntityFormController::entityFormSubmit(). + */ + public function entityFormSubmit(&$entity_form, &$form_state) { + parent::entityFormSubmit($entity_form, $form_state); + } + + /** + * Overrides EntityInlineEntityFormController::save(). + */ + public function save($entity, $context) { + bean_save($entity); + } +} \ No newline at end of file