Error message

  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1075 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1079 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1075 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1079 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1075 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1079 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1075 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1079 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1075 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1079 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1075 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1079 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).

function tmgmt_content_form_tmgmt_settings_form_alter

Implements hook_form_FORM_ID_alter() for tmgmt_settings_form().

Parameters

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

File

sources/content/tmgmt_content.module, line 109
Source plugin for the Translation Management system that handles entities.

Code

function tmgmt_content_form_tmgmt_settings_form_alter(array &$form, FormStateInterface $form_state) {
  \Drupal::moduleHandler()
    ->loadInclude('views', 'inc', 'views.views');
  $entity_types = \Drupal::entitytypeManager()
    ->getDefinitions();
  $form['content'] = array(
    '#type' => 'details',
    '#title' => t('Embedded references'),
    '#description' => t('All checked reference fields will automatically add the translatable data of the reference to the job. It is recommended to only use this when the targets are only used once, otherwise they might get translated multiple times.'),
    '#open' => TRUE,
  );
  $form['content']['embedded_fields'] = array(
    '#tree' => TRUE,
  );
  $content_translation_manager = \Drupal::service('content_translation.manager');

  // List of entity types that will be ignored in the list of embedded fiels.
  $ignored_target_types = [
    'user',
    'file',
  ];
  $always_embedded = [];
  foreach ($entity_types as $entity_type) {
    if ($content_translation_manager
      ->isEnabled($entity_type
      ->id())) {
      $field_options = array();
      $descriptions = [];
      $translatable_bundles = array_filter(array_keys(\Drupal::service('entity_type.bundle.info')
        ->getBundleInfo($entity_type
        ->id())), function ($bundle) use ($entity_type, $content_translation_manager) {
        return $content_translation_manager
          ->isEnabled($entity_type
          ->id(), $bundle);
      });
      $storage_definitions = \Drupal::service('entity_field.manager')
        ->getFieldStorageDefinitions($entity_type
        ->id());
      foreach ($storage_definitions as $field_name => $storage_definition) {

        // Filter out storage definitions that do not have at least one
        // field definition on a translatable bundle, flag those that are set to
        // translatable.
        $allowed_option = FALSE;
        $untranslatable_option = FALSE;
        foreach ($translatable_bundles as $bundle) {
          $field_definitions = \Drupal::service('entity_field.manager')
            ->getFieldDefinitions($entity_type
            ->id(), $bundle);
          if (isset($field_definitions[$field_name])) {
            $allowed_option = TRUE;
            if (!$field_definitions[$field_name]
              ->isTranslatable()) {
              $untranslatable_option = TRUE;
            }
            break;
          }
        }
        if (!$allowed_option) {
          continue;
        }
        $property_definitions = $storage_definition
          ->getPropertyDefinitions();
        foreach ($property_definitions as $property_definition) {

          // Look for entity_reference properties where the storage definition
          // has a target type setting and that is enabled for content
          // translation.
          // @todo Support dynamic entity references and make this more flexible
          //   in general.
          if (in_array($property_definition
            ->getDataType(), [
            'entity_reference',
            'entity_revision_reference',
          ]) && ($target_type_id = $storage_definition
            ->getSetting('target_type'))) {

            // Skip ignored target types.
            if (in_array($target_type_id, $ignored_target_types)) {
              continue;
            }
            $target_entity_type = \Drupal::entityTypeManager()
              ->getDefinition($target_type_id);
            $is_target_type_translatable = $content_translation_manager
              ->isEnabled($target_type_id);

            // Untranslatable fields with the untranslatable target types
            // are not supported.
            if (!$is_target_type_translatable && $untranslatable_option) {
              continue;
            }
            $name = $storage_definition
              ->getName();

            // @todo: Support base entity reference fields as embedded fields?
            $label = $storage_definition
              ->isBaseField() ? $storage_definition
              ->getLabel() : views_entity_field_label($entity_type
              ->id(), $name)[0];

            // Entity types with this key set are considered composite
            // entities and always embedded in others. Do not expose them as
            // their own item type.
            // @todo: Also support translatable always embedded fields?
            if ($target_entity_type
              ->get('entity_revision_parent_type_field') && $untranslatable_option) {
              $id = str_replace('.', '_', $storage_definition
                ->isBaseField() ? $name : $storage_definition
                ->id());
              $always_embedded[$id] = $entity_type
                ->getLabel() . ': ' . $label;
            }
            else {
              $field_options[$name] = t('@label (@target_type)', [
                '@label' => $label,
                '@target_type' => $target_entity_type
                  ->getLabel(),
              ]);
              if (!$is_target_type_translatable) {
                $descriptions[$name]['#description'] = t('Note: This is a translatable field to an untranslatable target. A copy of the target will be created when translating.');
              }
              elseif (!$untranslatable_option) {
                $descriptions[$name]['#description'] = t('Note: This is a translatable field, embedding this will add a translation on the existing reference.');
              }
            }
            break;
          }
        }
      }
      if (!empty($field_options)) {
        $default_value = array_keys((array) \Drupal::config('tmgmt_content.settings')
          ->get('embedded_fields.' . $entity_type
          ->id()));
        $form['content']['embedded_fields'][$entity_type
          ->id()] = array(
          '#type' => 'checkboxes',
          '#title' => $entity_type
            ->getLabel(),
          '#options' => $field_options,
          '#default_value' => $default_value,
        ) + $descriptions;
      }
    }
  }
  if (!empty($always_embedded)) {
    $form['content']['always_embedded'] = array(
      '#type' => 'item',
      '#title' => t('Always embedded'),
      '#description' => t('These references are always embedded in the translatable data.'),
      '#description_display' => 'after',
      'list' => array(
        '#theme' => 'item_list',
        '#items' => $always_embedded,
      ),
    );
  }
  if (\Drupal::moduleHandler()
    ->moduleExists('content_moderation')) {
    $form['workflows'] = [
      '#type' => 'details',
      '#title' => t('Default moderation states'),
      '#description' => t('Default moderation states for each workflow used in translations when Content Moderation is enabled.'),
      '#open' => TRUE,
    ];
    $form['workflows']['default_moderation_states'] = [
      '#tree' => TRUE,
    ];

    // Loop over all workflows.
    foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
      $options = [];
      $states = $workflow
        ->getTypePlugin()
        ->getStates();
      foreach ($states as $state_id => $state) {
        $options[$state_id] = $state
          ->label();
      }

      // Add a default moderation state select.
      $form['workflows']['default_moderation_states'][$workflow
        ->id()] = [
        '#title' => t($workflow
          ->label()),
        '#type' => 'select',
        '#options' => $options,
        '#default_value' => \Drupal::config('tmgmt_content.settings')
          ->get('default_moderation_states.' . $workflow
          ->id()),
        '#empty_option' => '- Same as source -',
      ];
    }
  }
  $form['#submit'][] = 'tmgmt_content_settings_submit';
}