Last active
July 14, 2020 11:11
-
-
Save anotherjames/551498054c84f514936c5bebb4c8e4ce to your computer and use it in GitHub Desktop.
Example Drupal 7 module for editing & translating a content type with hundreds of fields
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 | |
| /** | |
| * @file | |
| * Example Drupal 7 module for editing & translating a content type with | |
| * hundreds of fields. See | |
| * https://www.computerminds.co.uk/articles/editing-translating-over-100-fields | |
| */ | |
| /** | |
| * Implements hook_module_implements_alter(). | |
| */ | |
| function farmer_module_implements_alter(&$implementations, $hook) { | |
| switch ($hook) { | |
| case 'form_alter': | |
| // Our form alter needs to run between those of entity_translation and | |
| // ETSSEF. Note that this farmer module has no actual hook_form_alter(), | |
| // but uses hook_form_FORM_ID_alter(). | |
| if (isset($implementations['farmer']) && isset($implementations['etssef'])) { | |
| $own_group = $implementations['farmer']; | |
| unset($implementations['farmer']); | |
| $rebuild = array(); | |
| foreach ($implementations as $module => $farmer) { | |
| // Insert our implementation in just before ETSSEF, which will already | |
| // be after entity_translation. | |
| if ($module === 'etssef') { | |
| $rebuild['farmer'] = $own_group; | |
| } | |
| $rebuild[$module] = $farmer; | |
| } | |
| $implementations = $rebuild; | |
| } | |
| break; | |
| } | |
| } | |
| /** | |
| * Override the node form for farmer nodes only. | |
| * | |
| * @see hook_forms() | |
| * @see node_forms() | |
| * @see drupal_retrieve_form() | |
| */ | |
| function farmer_node_form($form, &$form_state, $node) { | |
| if (!isset($node->type) || $node->type !== 'farmer') { | |
| return node_form($form, $form_state, $node); | |
| } | |
| else { | |
| // @TODO Copy the entire contents of node_form() to here, but replace the | |
| // call to field_attach_form() with a call to field_attach_form_selective(). | |
| // Use the same arguments. | |
| return $form; | |
| } | |
| } | |
| /** | |
| * Implements hook_form_FORM_ID_alter(). | |
| * | |
| * This has to run after @code entity_translation_form_alter @endcode in order | |
| * for @code etssef_is_active() @endcode to return correctly, as the translation | |
| * form is only set up by the ET handler then. | |
| */ | |
| function farmer_farmer_node_form_alter(&$form, &$form_state, $form_id) { | |
| if (!empty($form['field_attach_form_selective']['field_attach_form_selective_field_selector']['#options']) && module_exists('etssef')) { | |
| if ($etssef_state = etssef_is_active()) { | |
| // Ensure selector shows regardless of whether this form is a translation | |
| // one, or the ETSSEF-powered shared one. | |
| $form['field_attach_form_selective']['#etssef_skip'] = TRUE; | |
| // The field selector will allow adding translatable fields when on a | |
| // translation form (whether for a new or existing translation). When on | |
| // the shared form, it only allows adding untranslatable ones, so it | |
| // should be flagged as non-multilingual (causing the 'All languages' | |
| // title suffix to show). | |
| $is_shared_form = $etssef_state === ETSSEF_STATE_ACTIVE; | |
| $form['field_attach_form_selective']['#multilingual'] = !$is_shared_form; | |
| // Filter the field options. | |
| $options = $form['field_attach_form_selective']['field_attach_form_selective_field_selector']['#options']; | |
| $options = array_filter($options, function ($field_name) use ($is_shared_form) { | |
| $field = field_info_field($field_name); | |
| if ($is_shared_form) { | |
| // On the shared form, retain untranslatable fields. | |
| return empty($field['translatable']); | |
| } | |
| else { | |
| // On translation forms, retain translatable fields. | |
| return !empty($field['translatable']); | |
| } | |
| }, ARRAY_FILTER_USE_KEY); | |
| $form['field_attach_form_selective']['field_attach_form_selective_field_selector']['#options'] = $options; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment