5 * Helper module for the Content Translation tests.
8 use Drupal\Core\Access\AccessResult;
9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Session\AccountInterface;
14 * Implements hook_entity_bundle_info_alter().
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
25 $bundles['entity_test_mul']['entity_test_mul']['translatable'] = TRUE;
30 * Implements hook_entity_access().
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();
38 return AccessResult::neutral();
43 * Implements hook_form_BASE_FORM_ID_alter().
45 * Adds a textfield to node forms based on a request parameter.
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',
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';
64 * Form submission handler for custom field added based on a request parameter.
66 * @see content_translation_test_form_node_article_form_alter()
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'));