You are here

public static function ConvertNodes::getFromFields in Convert Nodes 8

1 call to ConvertNodes::getFromFields()
ConvertNodesForm::buildForm in src/Form/ConvertNodesForm.php
Form constructor.

File

src/ConvertNodes.php, line 17

Class

ConvertNodes
ConvertNodes.

Namespace

Drupal\convert_nodes

Code

public static function getFromFields($fields_from, $fields_to_names, $fields_to_types) {
  $fields_from_names = [];
  $form = [];
  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,
  ];
}