You are here

public function FieldMappings::buildForm in Schema.org configuration tool (RDF UI) 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/FieldMappings.php, line 47

Class

FieldMappings
RDF UI Field Mapping form.

Namespace

Drupal\rdfui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL) {
  $this->entityTypeId = $entity_type_id;
  $this->bundle = $bundle;
  $this->rdfConverter = new SchemaOrgConverter();

  // Gather bundle information.
  $instances = array_filter(\Drupal::service('entity_field.manager')
    ->getFieldDefinitions($entity_type_id, $bundle), function ($field_definition) {
    return $field_definition instanceof FieldConfigInterface;
  });
  $mappings = rdf_get_mapping($this->entityTypeId, $this->bundle);
  $options = NULL;
  $bundle_mapping = $mappings
    ->getBundleMapping();
  if (!empty($bundle_mapping) && !empty($bundle_mapping['types']['0'])) {
    $type = $bundle_mapping['types']['0'];
    $options = $this->rdfConverter
      ->getTypeProperties($type);
  }
  else {
    $options = $this->rdfConverter
      ->getListProperties();
  }
  $form += array(
    '#entity_type' => $this->entityTypeId,
    '#bundle' => $this->bundle,
    '#fields' => array_keys($instances),
  );
  $table = array(
    '#type' => 'table',
    '#tree' => TRUE,
    '#header' => array(
      $this
        ->t('Label'),
      $this
        ->t('RDF Property'),
      $this
        ->t('Data Type'),
      $this
        ->t('Status'),
    ),
    '#regions' => $this
      ->getRegions(),
    '#attributes' => array(
      'class' => array(
        'rdfui-field-mappings',
      ),
      'id' => Html::getUniqueId('rdf-mapping'),
    ),
  );

  // Fields.
  foreach ($instances as $name => $instance) {
    $property = $mappings
      ->getFieldMapping($name);
    $table[$name] = array(
      '#attributes' => array(
        'id' => Html::getUniqueId($name),
      ),
      'label' => array(
        '#markup' => $this
          ->t($instance
          ->getLabel()),
      ),
      'rdf-predicate' => array(
        '#id' => 'rdf-predicate',
        '#type' => 'select',
        '#title' => $this
          ->t('RDF Property'),
        '#title_display' => 'invisible',
        '#options' => $options,
        '#empty_option' => '',
        '#attached' => array(
          'library' => array(
            'rdfui/drupal.rdfui.autocomplete',
          ),
        ),
        '#default_value' => !empty($property) ? $property['properties'][0] : '',
      ),
      'type' => array(
        '#title' => $this
          ->t('Data Type'),
        '#title_display' => 'invisible',
        '#markup' => $this
          ->t('Text'),
      ),
      'status' => array(
        '#title' => $this
          ->t('Status'),
        '#title_display' => 'invisible',
        '#markup' => !empty($property['properties'][0]) ? 'Mapped' : 'Unmapped',
      ),
    );
  }
  $table['#regions']['content']['rows_order'] = array();
  foreach (Element::children($table) as $name) {
    $table['#regions']['content']['rows_order'][] = $name;
  }
  $form['fields'] = $table;
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#button_type' => 'primary',
    '#value' => $this
      ->t('Save'),
  );
  return $form;
}