You are here

function field_inheritance_form_alter in Field Inheritance 8

Same name and namespace in other branches
  1. 2.0.x field_inheritance.module \field_inheritance_form_alter()

Implements hook_form_alter().

File

./field_inheritance.module, line 134
Contains field_inheritance.module.

Code

function field_inheritance_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form_object = $form_state
    ->getFormObject();
  if (!empty($form_object) && $form_object instanceof EntityForm) {
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
    if (!empty($entity) && $entity instanceof FieldableEntityInterface && strpos($form['#id'], 'delete') === FALSE) {
      $entity_type = $entity
        ->getEntityTypeId();
      $bundle = $entity
        ->bundle();
      $config = \Drupal::config('field_inheritance.config');
      $included_entities = $config
        ->get('included_entities');
      $included_bundles = $config
        ->get('included_bundles');
      if (!empty($included_entities)) {
        $included_entities = explode(',', $included_entities);
        if (array_search($entity_type, $included_entities) === FALSE) {
          return;
        }
      }
      if (!empty($included_bundles)) {
        $included_bundles = explode(',', $included_bundles);
        if (array_search($entity_type . ':' . $bundle, $included_bundles) === FALSE) {
          return;
        }
      }
      $state_key = $entity
        ->getEntityTypeId() . ':' . $entity
        ->uuid();
      $state = \Drupal::keyValue('field_inheritance');
      $state_values = $state
        ->get($state_key);
      $form['actions']['submit']['#submit'][] = 'field_inheritance_entity_form_submit';
      $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
      $form['field_inheritance'] = [
        '#type' => 'details',
        '#open' => TRUE,
        '#title' => t('Field Inheritance'),
        '#attributes' => [
          'id' => 'field-inheritance--wrapper',
        ],
      ];
      $form['field_inheritance']['field_inheritance_enable'] = [
        '#type' => 'checkbox',
        '#title' => t('Enable inheritance for this entity?'),
        '#description' => t('Enabling inheritance will allow data to be pulled in from a source entity into this entity.'),
        '#default_value' => $state_values['enabled'] ?? NULL,
      ];
      $form['field_inheritance']['fields'] = [
        '#type' => 'fieldset',
        '#title' => t('Field Mappings'),
        '#states' => [
          'visible' => [
            'input[name="field_inheritance_enable"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $inherited_field_ids = \Drupal::entityQuery('field_inheritance')
        ->condition('destinationEntityType', $entity_type)
        ->condition('destinationEntityBundle', $bundle)
        ->execute();
      if (!empty($inherited_field_ids)) {
        $inherited_fields = \Drupal::entityTypeManager()
          ->getStorage('field_inheritance')
          ->loadMultiple($inherited_field_ids);
        if (!empty($inherited_fields)) {
          foreach ($inherited_fields as $field) {
            $form['field_inheritance']['fields']['field_inheritance_' . $field
              ->idWithoutTypeAndBundle()] = [
              '#type' => 'details',
              '#open' => TRUE,
              '#title' => t('Entity Mapping for @label [@entity: @bundle - @field]', [
                '@label' => $field
                  ->label(),
                '@entity' => $field
                  ->sourceEntityType(),
                '@bundle' => $field
                  ->sourceEntityBundle(),
                '@field' => $field
                  ->sourceField(),
              ]),
              'field_inheritance_field_skip_' . $field
                ->idWithoutTypeAndBundle() => [
                '#type' => 'checkbox',
                '#title' => t('Do not inherit this field'),
                '#description' => t('Checking this box will prevent data from being inherited for this field on this entity.'),
                '#default_value' => $state_values[$field
                  ->idWithoutTypeAndBundle()]['skip'] ?? FALSE,
              ],
              'field_inheritance_field_entity_' . $field
                ->idWithoutTypeAndBundle() => [
                '#type' => 'entity_autocomplete',
                '#title' => t('Source Entity'),
                '#description' => t('Enter the name of the %bundle from which to inherit data.', [
                  '%bundle' => $field
                    ->sourceEntityBundle(),
                ]),
                '#target_type' => $field
                  ->sourceEntityType(),
                '#selection_settings' => [
                  'target_bundles' => [
                    $field
                      ->sourceEntityBundle(),
                  ],
                ],
                '#default_value' => !empty($state_values[$field
                  ->idWithoutTypeAndBundle()]['entity']) ? \Drupal::entityTypeManager()
                  ->getStorage($field
                  ->sourceEntityType())
                  ->load($state_values[$field
                  ->idWithoutTypeAndBundle()]['entity']) : FALSE,
                '#states' => [
                  'invisible' => [
                    'input[name="field_inheritance_field_skip_' . $field
                      ->idWithoutTypeAndBundle() . '"]' => [
                      'checked' => TRUE,
                    ],
                  ],
                ],
              ],
            ];
          }
        }
      }
      $form['field_inheritance']['fields']['message'] = [
        '#type' => 'markup',
        '#markup' => '<div id="field-inheritance-ajax-message"></div>',
      ];
      $form['field_inheritance']['fields']['ajax_container'] = [
        '#type' => 'container',
        '#attributes' => [
          'id' => 'field-inheritance-ajax-container',
        ],
      ];
      $form['field_inheritance']['fields']['add'] = [
        '#type' => 'fieldset',
        '#access' => \Drupal::currentUser()
          ->hasPermission('administer field inheritance'),
        'message' => [
          '#type' => 'markup',
          '#prefix' => '<p>',
          '#markup' => t('Add a new field inheritance field to this entity type and bundle.'),
          '#suffix' => '</p>',
        ],
        'add_field_inheritance' => [
          '#type' => 'link',
          '#title' => t('Add Field Inheritance'),
          '#url' => Url::fromRoute('field_inheritance.creation_form', [
            'entity_type' => $entity
              ->getEntityTypeId(),
            'entity_bundle' => $entity
              ->bundle(),
          ]),
          '#attributes' => [
            'class' => [
              'use-ajax',
              'button',
              'button--small',
            ],
            'data-dialog-type' => 'modal',
            'data-dialog-options' => Json::encode([
              'width' => '60%',
            ]),
          ],
        ],
      ];
    }
  }
}