Created
September 15, 2015 03:52
-
-
Save doublejosh/c442a2aa8a2309039e92 to your computer and use it in GitHub Desktop.
Entity form rabbit hole dead end
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Add entity data item for component and modifier. | |
| function componentize_fields_entity_info_alter(&$entity_info) { | |
| dsm($entity_info); | |
| } | |
| /** | |
| * Implements hook_form_alter(). | |
| * | |
| * @param array $form | |
| * @param array $form_state | |
| * @param string $form_id | |
| */ | |
| function componentize_fields_form_alter(&$form, &$form_state, $form_id) { | |
| // Bundle settings forms. | |
| $special_type_form_ids = array( | |
| 'node_type_form', | |
| //'taxonomy_form_vocabulary', | |
| //'profile2_type_overview_form' | |
| ); | |
| if (preg_match('/.*_admin_bundle_form$/', $form_id) || in_array($form_id, $special_type_form_ids)) { | |
| _componentize_fields_bundle_settings_form_elements($form); | |
| } | |
| // Bundle edit form. | |
| // if (preg_match('/.*_node_form$/', $form_id)) { | |
| // _componentize_fields_entity_settings_form_elements($form); | |
| // } | |
| //'_entityform_edit_form' | |
| } | |
| // Get settings. | |
| $settings = ctools_export_crud_load_all('componentize_fields_entity_view_mode'); | |
| $settings[$form['#entity_type'] . '-' . $form['#bundle']]; | |
| _componentize_build_modifier_list_form($form, array(), $default_value); | |
| /** | |
| * Common utility to add form element to allow modifier setting per entity. | |
| * | |
| * @param array $form | |
| */ | |
| function _componentize_fields_bundle_settings_form_elements(&$form) { | |
| $settings = variable_get('componentize_fields_modifer', array()); | |
| // Carefully grab current setting from shared variable. | |
| $bundle_setting = FALSE; | |
| if (isset($settings[$form['#entity_type']]) && isset($settings[$form['#entity_type']][$form['#bundle']])) { | |
| $bundle_setting = $settings[$form['#entity_type']][$form['#bundle']]; | |
| } | |
| // Add form components. | |
| $form['componentize'] = array( | |
| '#type' => 'fieldset', | |
| '#title' => t('Components'), | |
| '#collapsible' => TRUE, | |
| '#collapsed' => FALSE, | |
| '#weight' => 10, | |
| ); | |
| $form['componentize']['modifiers'] = array( | |
| '#type' => 'checkbox', | |
| '#title' => t('Allow setting modifier'), | |
| '#description' => t('Collect per-entity component modifier from privileged users. Configure usage within: Manage display.'), | |
| '#default_value' => $bundle_setting, | |
| ); | |
| // Vertical tabs on node type forms. | |
| if(isset($form['additional_settings']) && $form['additional_settings']['#type'] === 'vertical_tabs') { | |
| $form['componentize']['#group'] = 'additional_settings'; | |
| } | |
| $form['#submit'][] = 'componentize_fields_bundle_settings_submit'; | |
| } | |
| /** | |
| * Submit handler for bundle settings. Currently: allowing modifier collection. | |
| */ | |
| function componentize_fields_bundle_settings_submit($form, &$form_state) { | |
| // Store bundle setting. | |
| dsm($form_state); | |
| // Add or remove modifier setting field instance. | |
| if ($instance = field_info_instance($form['entity_type'], 'field_componentize_modifier', $form['bundle'])) { | |
| } | |
| // // Remove the base field. | |
| // field_delete_field('field_componentize_modifier'); | |
| // // Create the field instance on the bundle. | |
| // $instance = array( | |
| // 'field_name' => 'field_myField', | |
| // 'entity_type' => 'user', | |
| // 'label' => 'My Field Name', | |
| // 'bundle' => 'user', | |
| // // If you don't set the "required" property then the field wont be required by default. | |
| // 'required' => TRUE, | |
| // 'settings' => array( | |
| // // Here you inform either or not you want this field showing up on the registration form. | |
| // 'user_register_form' => 1, | |
| // ), | |
| // 'widget' => array( | |
| // 'type' => 'textfield', | |
| // ), | |
| // ); | |
| // field_create_instance($instance); | |
| } | |
| /** | |
| * Implements hook_entity_load(). | |
| */ | |
| function componentize_fields_entity_load($entities, $type) { | |
| dsm($entities); | |
| dsm($type); | |
| } | |
| /** | |
| * Utility for getting bundle component settings. | |
| * | |
| * @param string $bundle | |
| * @param string $setting | |
| * | |
| * @return mixed | |
| */ | |
| function _componentize_fields_get_bundle_settings($entity_type, $bundle, $setting) { | |
| $settings = variable_get('componentize_fields_' . $setting, array()); | |
| if (isset($settings[$entity_type]) && isset($settings[$entity_type][$bundle])) { | |
| return $settings[$entity_type][$bundle]; | |
| } | |
| return FALSE; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment