Commit cbea8496 authored by Joseph Olstad's avatar Joseph Olstad
Browse files

Issue #3284345 by joseph.olstad: Enabling a content type with etuf can throw...

Issue #3284345 by joseph.olstad: Enabling a content type with etuf can throw an undefined index line 168 of entity_translation_unified_form.module
parent e779aaf8
Loading
Loading
Loading
Loading
+68 −43
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
 * @file
 */

use Drupal\field\Entity\FieldConfig;
use Drupal\content_translation\BundleTranslationSettingsInterface;
use Drupal\Core\Access\AccessResultForbidden;
use Drupal\Core\Entity\EntityForm;
@@ -44,8 +45,10 @@ function entity_translation_unified_form_preprocess_html(&$variables) {
  $patterns = "/node/add/*\n/node/*/edit";
  $match = \Drupal::service('path.matcher')->matchPath($current_path, $patterns);
  if (!empty($match)) {
    $variables['attributes']['class'][] = 'etuf-sbs'; // For css.
    $variables['attributes']['class'][] = 'sync'; // For sync js.
    // For css.
    $variables['attributes']['class'][] = 'etuf-sbs';
    // For sync js.
    $variables['attributes']['class'][] = 'sync';
  }
}

@@ -61,7 +64,7 @@ function entity_translation_unified_form_page_attachments(array &$page) {
  if (!empty($match)) {
    $is_valid = \Drupal::service('path.validator')->isValid($current_path);
    if (!$is_valid) {
     return new AccessResultForbidden;
      return new AccessResultForbidden();
    }
    $bundle = '';
    if (strpos($current_path, '/edit') > 4) {
@@ -136,6 +139,24 @@ function entity_translation_unified_form_form_node_type_form_process(&$form, $fo
    '#disabled' => FALSE,
    '#states' => $states,
  ];
  $form['workflow']['entity_translation_unified_form']['entity_translation_unified_form_translate_labels'] = [
    '#title' => t('Translate fields labels and descriptions in each language'),
    '#type' => 'checkbox',
    '#default_value' => entity_translation_unified_form_translate_labels_enabled($entity_type_id, $bundle),
    '#states' => [
      'visible' => [
        ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_enable]"]'
        => ['checked' => TRUE],
        'and',
        ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_theme]"]'
        => ['value' => 'EntityTranslationUnifiedFormInlineMode'],
        'and',
        ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['workflow']['entity_translation_unified_form']['entity_translation_unified_form_sbs_enable'] = [
    '#title' => t('Enable side-by-side UI mode on the node add/edit form'),
    '#type' => 'checkbox',
@@ -151,7 +172,6 @@ function entity_translation_unified_form_form_node_type_form_process(&$form, $fo
    '#states' => $states,
  ];


  return $form;
}

@@ -167,7 +187,10 @@ function entity_translation_unified_form_form_node_type_form_submit(&$form, Form
  entity_translation_unified_form_set_sbs_enabled($entity_type_id, $bundle, $enabled);
  $enabled = $form_state->getValues()['entity_translation_unified_form_moderation_sync_disable'];
  entity_translation_unified_form_set_moderation_sync_disabled($entity_type_id, $bundle, $enabled);
  // $enabled = $form_state->getValues()['entity_translation_unified_form_translate_labels'];
  if (isset($form_state->getValues()['entity_translation_unified_form_translate_labels'])) {
    $enabled = $form_state->getValues()['entity_translation_unified_form_translate_labels'];
  }
  entity_translation_unified_form_set_translate_labels_enabled($entity_type_id, $bundle, $enabled);
}

@@ -310,7 +333,7 @@ function entity_translation_unified_form_bundle_display_mode($entity_type_id, $b
}

/**
 * Returns the language display mode for a given bundle
 * Returns the language display mode for a given bundle.
 */
function entity_translation_unified_form_language_display($entity_type_id, $bundle) {
  /** @var \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager */
@@ -326,10 +349,10 @@ function entity_translation_unified_form_language_display($entity_type_id, $bund
}

/**
 * Returns the different display modes for the language part
 * Returns the different display modes for the language part.
 */
function entity_translation_unified_form_get_language_display_options() {
  // TODO: make constants rather than strings
  // @todo make constants rather than strings
  return [
    'current' => t('Current language'),
    'native' => t('Native language'),
@@ -435,10 +458,10 @@ function entity_translation_unified_form_form_language_content_settings_form_alt
          '#states' => [
            'visible' => [
              ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_enable]"]' =>
              [ 'checked' => TRUE, ],
              ['checked' => TRUE],
              'and',
              ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_theme]"]' =>
              [ 'value' => 'EntityTranslationUnifiedFormInlineMode', ],
              ['value' => 'EntityTranslationUnifiedFormInlineMode'],
              'and',
              ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [
                'checked' => TRUE,
@@ -453,10 +476,10 @@ function entity_translation_unified_form_form_language_content_settings_form_alt
          '#states' => [
            'visible' => [
              ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_enable]"]' =>
              [ 'checked' => TRUE, ],
              ['checked' => TRUE],
              'and',
              ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_theme]"]' =>
              [ 'value' => 'EntityTranslationUnifiedFormInlineMode', ],
              ['value' => 'EntityTranslationUnifiedFormInlineMode'],
              'and',
              ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [
                'checked' => TRUE,
@@ -476,7 +499,7 @@ function entity_translation_unified_form_form_language_content_settings_form_alt
                ],
                'and',
                ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][settings][content_translation][entity_translation_unified_form_enable]"]' =>
                [ 'checked' => TRUE, ],
                ['checked' => TRUE],
              ],
            ],
          ];
@@ -614,7 +637,7 @@ function entity_translation_unified_form_add_fields(&$form, FormStateInterface $
/**
 * Instanciate one value (for default_value)
 */
function entity_translation_unified_form_def_value($val, $mono=false) {
function entity_translation_unified_form_def_value($val, $mono = FALSE) {
  if ($mono) {
    return $val;
  }
@@ -622,18 +645,19 @@ function entity_translation_unified_form_def_value($val, $mono=false) {
    return $val;
  }
  if (isset($val['date'])) {
    return $val['date']; // date only needs date object
    // Date only needs date object.
    return $val['date'];
  }
  if (count($val) == 1) {
    return current($val);
  }
  // this probably wont work in all cases
  // This probably wont work in all cases.
  return $val;
}

/**
 * Create a field definition from the existing one. Checks about the fields should have been
 * done before calling this
 * done before calling this.
 */
function entity_translation_unified_form_build_field($form, $form_state, $entity, $entity_type, $bundle, $field_name, $langcode) {
  $language_manager = \Drupal::languageManager();
@@ -696,7 +720,7 @@ function entity_translation_unified_form_build_field($form, $form_state, $entity
  }
  $widget = $form_display->getRenderer($field_name);
  if (empty($widget)) {
    // restore language
    // Restore language.
    if ($translate_labels) {
      $language_manager->setConfigOverrideLanguage($initial_language);
      \Drupal::service("entity_field.manager")->clearCachedFieldDefinitions();
@@ -704,16 +728,16 @@ function entity_translation_unified_form_build_field($form, $form_state, $entity
    return [];
  }
  $field_form = $widget->form($items, $form, $form_state);
  // manage default value (because Drupal is not able to set the default_value that matches the
  // Manage default value (because Drupal is not able to set the default_value that matches the
  // forced language, so it has to be done by hand…). This is probably incomplete.
  if ($entity->isNew()) {
    // get default value (at least 'title' field don't have any definition)
    // Get default value (at least 'title' field don't have any definition)
    if (!$translate_labels) {
      // default value must come with forced language
      // Default value must come with forced language.
      $language_manager->setConfigOverrideLanguage($language_manager->getLanguage($langcode));
      \Drupal::service("entity_field.manager")->clearCachedFieldDefinitions();
    }
    $field_definition = \Drupal\field\Entity\FieldConfig::loadByName($entity_type, $bundle, $field_name);
    $field_definition = FieldConfig::loadByName($entity_type, $bundle, $field_name);
    if ($field_definition) {
      $def = $field_definition->getDefaultValue($entity);
    }
@@ -726,7 +750,7 @@ function entity_translation_unified_form_build_field($form, $form_state, $entity
    }
    if (isset($def) and !empty($def)) {
      if (isset($field_form['widget']['#default_value'])) {
        $field_form['widget']['#default_value'] = entity_translation_unified_form_def_value($def, true);
        $field_form['widget']['#default_value'] = entity_translation_unified_form_def_value($def, TRUE);
      }
      else {
        foreach ($def as $idx => $cont) {
@@ -739,7 +763,7 @@ function entity_translation_unified_form_build_field($form, $form_state, $entity
      }
    }
  }
  // restore language
  // Restore language.
  if ($translate_labels) {
    $language_manager->setConfigOverrideLanguage($initial_language);
    \Drupal::service("entity_field.manager")->clearCachedFieldDefinitions();
@@ -775,7 +799,7 @@ function entity_translation_unified_form_node_insert_other_language_fields(&$for
      $other_languages = EtufHelper::getOtherEnabledLanguages();
      foreach ($other_languages as $other_langcode => $other_language) {
        $current_language = $language_manager->getLanguage($other_langcode);
        // Get the field definition for this language
        // Get the field definition for this language.
        $field_form = entity_translation_unified_form_build_field($form, $form_state, $entity, $entity_type, $bundle, $field_name, $other_langcode);
        // Build and attach the translated field.
        if (!empty($field_form)) {
@@ -837,7 +861,8 @@ function entity_translation_unified_form_node_insert_other_language_fields(&$for
      if (!empty($field_group_wrapper)) {
        $form[$field_name]['#theme_wrappers'][] = $field_group_wrapper;
      }
    } elseif (entity_translation_unified_form_sbs_enabled($entity_type, $bundle) && !$hidden) {
    }
    elseif (entity_translation_unified_form_sbs_enabled($entity_type, $bundle) && !$hidden) {
      // In case of side-by-side add a wrapper to prevent moving to the right in case of field height change.
      if (isset($form[$field_name]['#prefix'])) {
        $form[$field_name]['#prefix'] = '<div class="etuf-sbs-none">' . $form[$field_name]['#prefix'];
@@ -1238,7 +1263,8 @@ function _custom_shutdown_menu($entity, $langcode) {
  }
  else {
    // Ensure base_url is used, to make this solution work for everyone.
    $url = $url;//$base_url . $url;
    // $base_url . $url;.
    $url = $url;
  }
  $debug = FALSE;
  EtufHelper::addToLog('path:' . $path, $debug);
@@ -1361,4 +1387,3 @@ function etuf_latest_revision($id, &$vid, $lang, $entity_type = 'node') {
  }
  return FALSE;
}
+4 −1
Original line number Diff line number Diff line
<?php

namespace Drupal\entity_translation_unified_form\Annotation;

use Drupal\Component\Annotation\Plugin;

/**
 * Defines a EntityTranslationUnifiedFormMode annotation object.
 *
 * @Annotation
 */
class EntityTranslationUnifiedFormMode extends Plugin {
@@ -24,4 +26,5 @@ class EntityTranslationUnifiedFormMode extends Plugin {
   * @ingroup plugin_translatable
   */
  public $admin_label = '';

}
+5 −2
Original line number Diff line number Diff line
@@ -2,11 +2,13 @@

namespace Drupal\entity_translation_unified_form;

use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;

/**
 *
 */
class EntityTranslationUnifiedFormModePluginManager extends DefaultPluginManager {

  /**
@@ -22,4 +24,5 @@ class EntityTranslationUnifiedFormModePluginManager extends DefaultPluginManager

    $this->setCacheBackend($cache_backend, 'entity_translation_unified_form_plugins');
  }

}
+25 −11
Original line number Diff line number Diff line
@@ -12,14 +12,24 @@ use Drupal\entity_translation_unified_form\EntityTranslationUnifiedFormModeInter
 * )
 */
class EntityTranslationUnifiedFormInlineMode implements EntityTranslationUnifiedFormModeInterface {

  /**
   *
   */
  public function fieldFormAlter($form, $form_state, &$field, $field_name, $language) {
    $this->alterTitle($form, $form_state, $field, $field_name, $language);
  }

  /**
   *
   */
  public function getFieldGroupThemeWrapper($form, $form_state, $field, $field_name) {
    return 'entity_translation_unified_form__inline__wrapper';
  }

  /**
   *
   */
  public function getFieldThemeWrapper($form, $form_state, $field, $field_name, $language) {
    return 'entity_translation_unified_form__inline__field_wrapper';
  }
@@ -31,7 +41,7 @@ class EntityTranslationUnifiedFormInlineMode implements EntityTranslationUnified
    if (!isset($field['widget']) || !is_object($language)) {
      return;
    }
    // Get language display mode
    // Get language display mode.
    $form_object = $form_state->getFormObject();
    $entity = $form_object->getEntity();
    $entity_type_id = $entity->getEntityTypeId();
@@ -45,9 +55,11 @@ class EntityTranslationUnifiedFormInlineMode implements EntityTranslationUnified
      $current_language = $native_languages[$language->getId()];
      // Get language name in its own language.
      $text = ' (' . t($current_language->getName(), [], ['langcode' => $language->getId()]) . ')';
    } elseif ($language_display == 'current') {
    }
    elseif ($language_display == 'current') {
      $text = ' (' . t($language->getName()) . ')';
    } else {
    }
    else {
      $text = ' (' . $language->getId() . ')';
    }
    $widget = &$field['widget'];
@@ -64,11 +76,11 @@ class EntityTranslationUnifiedFormInlineMode implements EntityTranslationUnified
   * @param array $element
   *   A form element array.
   *
   * This was taken and modified from content_translation
   *   This was taken and modified from content_translation.
   */
  protected function addTranslatabilityClue(&$element, $suffix) {
    static $fapi_title_elements;
    $done = true;
    $done = TRUE;

    // Elements which can have a #title attribute according to FAPI Reference.
    if (!isset($fapi_title_elements)) {
@@ -83,9 +95,10 @@ class EntityTranslationUnifiedFormInlineMode implements EntityTranslationUnified
      $element['#title'] .= $suffix;
      // If we changed a invisible title the job is not done.
      if (isset($element['#title_display']) && ($element['#title_display'] == 'invisible')) {
        return false;
      } else {
        return true;
        return FALSE;
      }
      else {
        return TRUE;
      }
    }

@@ -93,7 +106,7 @@ class EntityTranslationUnifiedFormInlineMode implements EntityTranslationUnified
    if ($children = Element::children($element)) {
      foreach ($children as $delta) {
        if (!$this->addTranslatabilityClue($element[$delta], $suffix)) {
          $done = false;
          $done = FALSE;
        }
      }
    }
@@ -102,4 +115,5 @@ class EntityTranslationUnifiedFormInlineMode implements EntityTranslationUnified
      $element['#title'] .= $suffix;
    }
  }

}
+22 −9
Original line number Diff line number Diff line
@@ -11,28 +11,41 @@ use Drupal\entity_translation_unified_form\EntityTranslationUnifiedFormModeInter
 * )
 */
class EntityTranslationUnifiedFormTabbedMode extends EntityTranslationUnifiedFormInlineMode implements EntityTranslationUnifiedFormModeInterface {

  /**
   *
   */
  public function fieldFormAlter($form, $form_state, &$field, $field_name, $language) {
    if ($field_name == 'path') {
      // path doesn't currently work well with tabs.
      // use the inline mode for now
      // Path doesn't currently work well with tabs.
      // use the inline mode for now.
      parent::alterTitle($form, $form_state, $field, $field_name, $language);
    }
    return;
  }

  /**
   *
   */
  public function getFieldGroupThemeWrapper($form, $form_state, $field, $field_name) {
    if ($field_name == 'path') {
      // path doesn't currently work well with tabs.
      // use the inline wrapper for now
      // Path doesn't currently work well with tabs.
      // use the inline wrapper for now.
      return 'entity_translation_unified_form__inline__wrapper';
    }
    return 'entity_translation_unified_form__a11y_accordion_tabs__wrapper';
  }

  /**
   *
   */
  public function getFieldThemeWrapper($form, $form_state, $field, $field_name, $language) {
    if ($field_name == 'path') {
      // path doesn't currently work well with tabs.
      // use the inline wrapper for now
      // Path doesn't currently work well with tabs.
      // use the inline wrapper for now.
      return 'entity_translation_unified_form__inline__field_wrapper';
    }
    return 'entity_translation_unified_form__a11y_accordion_tabs__field_wrapper';
  }

}