50495a8c799b202413954134d27803e6f8fc89ca
[yaffs-website] / web / core / modules / content_translation / tests / modules / content_translation_test / content_translation_test.module
1 <?php
2
3 /**
4  * @file
5  * Helper module for the Content Translation tests.
6  */
7
8 use Drupal\Core\Access\AccessResult;
9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Session\AccountInterface;
12
13 /**
14  * Implements hook_entity_bundle_info_alter().
15  */
16 function content_translation_test_entity_bundle_info_alter(&$bundles) {
17   // Store the initial status of the "translatable" property for the
18   // "entity_test_mul" bundle.
19   $translatable = !empty($bundles['entity_test_mul']['entity_test_mul']['translatable']);
20   \Drupal::state()->set('content_translation_test.translatable', $translatable);
21   // Make it translatable if Content Translation did not. This will make the
22   // entity object translatable even if it is disabled in Content Translation
23   // settings.
24   if (!$translatable) {
25     $bundles['entity_test_mul']['entity_test_mul']['translatable'] = TRUE;
26   }
27 }
28
29 /**
30  * Implements hook_entity_access().
31  */
32 function content_translation_test_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
33   $access = \Drupal::state()->get('content_translation.entity_access.' . $entity->getEntityTypeId());
34   if (!empty($access[$operation])) {
35     return AccessResult::allowed();
36   }
37   else {
38     return AccessResult::neutral();
39   }
40 }
41
42 /**
43  * Implements hook_form_BASE_FORM_ID_alter().
44  *
45  * Adds a textfield to node forms based on a request parameter.
46  */
47 function content_translation_test_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
48   $langcode = $form_state->getFormObject()->getFormLangcode($form_state);
49   if (in_array($langcode, ['en', 'fr']) && \Drupal::request()->get('test_field_only_en_fr')) {
50     $form['test_field_only_en_fr'] = [
51       '#type' => 'textfield',
52       '#title' => 'Field only available on the english and french form',
53     ];
54
55     foreach (array_keys($form['actions']) as $action) {
56       if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
57         $form['actions'][$action]['#submit'][] = 'content_translation_test_form_node_form_submit';
58       }
59     }
60   }
61 }
62
63 /**
64  * Form submission handler for custom field added based on a request parameter.
65  *
66  * @see content_translation_test_form_node_article_form_alter()
67  */
68 function content_translation_test_form_node_form_submit($form, FormStateInterface $form_state) {
69   \Drupal::state()->set('test_field_only_en_fr', $form_state->getValue('test_field_only_en_fr'));
70 }