acquia_vwo-1.0.x-dev/src/Form/FieldMappingForm.php

src/Form/FieldMappingForm.php
<?php

namespace Drupal\acquia_vwo\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Acquia VWO Settings form.
 */
class FieldMappingForm extends ConfigFormBase {

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  private $entityFieldManager;

  /**
   * Constructs an FieldMappingForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
   *   The typed configuration manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity manager.
   */
  public function __construct(
    ConfigFactoryInterface $config_factory,
    TypedConfigManagerInterface $typed_config,
    EntityFieldManagerInterface $entity_field_manager,
  ) {
    parent::__construct($config_factory, $typed_config);
    $this->entityFieldManager = $entity_field_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('config.typed'),
      $container->get('entity_field.manager'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'acquia_vwo_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    $h[] = $this->t('By mapping common taxonomies from your content types to these fields you can provide additional meta data to VWO.');
    $h[] = $this->t('This data can then be used to segment and target your audience. Note that these fields will only be captured by VWO on nodes and not views and other entities.');
    $form['help'] = [
      '#markup' => '<p>' . implode('</p><p>', $h) . '</p>',
    ];

    $field_names = $this->getTaxonomyTermFieldNames();

    $form['content_section'] = [
      '#type' => 'select',
      '#title' => $this->t('Content Section'),
      '#empty_value' => '',
      '#options' => $field_names,
      '#config_target' => 'acquia_vwo.settings:field_mapping.content_section',
    ];

    $form['content_keywords'] = [
      '#type' => 'select',
      '#title' => $this->t('Content Keywords'),
      '#empty_value' => '',
      '#options' => $field_names,
      '#config_target' => 'acquia_vwo.settings:field_mapping.content_keywords',
    ];

    $form['persona'] = [
      '#type' => 'select',
      '#title' => $this->t('Persona'),
      '#empty_value' => '',
      '#options' => $field_names,
      '#config_target' => 'acquia_vwo.settings:field_mapping.persona',
    ];

    return $form;
  }

  /**
   * Get a list of Field names that are targeting type Taxonomy Terms.
   *
   * @return array
   *   An array of field names.
   */
  private function getTaxonomyTermFieldNames() {
    $definitions = $this->entityFieldManager->getFieldStorageDefinitions('node');
    $field_names = [];
    foreach ($definitions as $field_name => $field_storage) {
      if ($field_storage->getType() != 'entity_reference' || $field_storage->getSetting('target_type') !== 'taxonomy_term') {
        continue;
      }
      $field_names[$field_name] = $field_name;
    }

    return $field_names;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'acquia_vwo.settings',
    ];
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc