You are here

public static function ConvertBundles::getFromFields in Convert Bundles 8

1 call to ConvertBundles::getFromFields()
ConvertBundlesForm::buildForm in src/Form/ConvertBundlesForm.php
Form constructor.

File

src/ConvertBundles.php, line 34

Class

ConvertBundles
ConvertBundles.

Namespace

Drupal\convert_bundles

Code

public static function getFromFields($fields_from_bundle, $fields_to_names, $fields_to_types) {
  $fields_from_names = [];
  $form = [];
  foreach ($fields_from_bundle as $fields_from) {
    foreach ($fields_from as $field) {
      $options = $fields_to_names;
      foreach ($options as $option => $label) {

        // Because might be target_id.
        $val_name = $field
          ->getFieldStorageDefinition()
          ->getMainPropertyName();

        // Because some data types are more complex.
        $data_type = '';
        $type = $field
          ->getType();
        if (empty($field
          ->getFieldStorageDefinition()
          ->getPropertyDefinition($val_name))) {
          $data_type = $type;
        }
        else {
          $data_type = $field
            ->getFieldStorageDefinition()
            ->getPropertyDefinition($val_name)
            ->getDataType();
        }
        if (!in_array($option, [
          'remove',
          'append_to_body',
        ]) && $fields_to_types[$option] != $data_type) {
          unset($options[$option]);
        }
      }
      if ($field
        ->getFieldStorageDefinition()
        ->isBaseField() == FALSE) {
        $fields_from_names[] = $field
          ->getName();
        $form[$field
          ->getName()] = [
          '#type' => 'select',
          '#title' => t('From Field [@field_name] @field_label:<br/> To Field', [
            '@field_name' => $field
              ->getName(),
            '@field_label' => is_object($field
              ->getLabel()) ? $field
              ->getLabel()
              ->render() : $field
              ->getLabel(),
          ]),
          '#options' => $options,
          '#default_value' => array_key_exists($field
            ->getName(), $fields_to_names) ? $field
            ->getName() : NULL,
        ];
      }
    }
  }
  return [
    'fields_from_names' => $fields_from_names,
    'fields_from_form' => $form,
  ];
}