You are here

class CreateTemplateForm in GatherContent 8.5

Hierarchy

Expanded class hierarchy of CreateTemplateForm

1 string reference to 'CreateTemplateForm'
gathercontent_upload_ui.routing.yml in gathercontent_upload_ui/gathercontent_upload_ui.routing.yml
gathercontent_upload_ui/gathercontent_upload_ui.routing.yml

File

gathercontent_upload_ui/src/Form/CreateTemplateForm.php, line 13

Namespace

Drupal\gathercontent_upload_ui\Form
View source
class CreateTemplateForm extends FormBase {

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity bundle info.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * Mapping creator.
   *
   * @var \Drupal\gathercontent_upload\Export\MappingCreator
   */
  protected $mappingCreator;

  /**
   * MappingCreator constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
   * @param \Drupal\gathercontent_upload\Export\MappingCreator $mappingCreator
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfoInterface $entityTypeBundleInfo, MappingCreator $mappingCreator) {
    $this->entityTypeManager = $entityTypeManager;
    $this->entityTypeBundleInfo = $entityTypeBundleInfo;
    $this->mappingCreator = $mappingCreator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('entity_type.bundle.info'), $container
      ->get('gathercontent_upload.mapping_creator'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['gathercontent']['entity_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Drupal entity type'),
      '#options' => $this
        ->getEntityTypes(),
      '#required' => TRUE,
      '#wrapper_attributes' => [
        'class' => [
          'inline-label',
        ],
      ],
      '#ajax' => [
        'callback' => '::getContentTypes',
        'wrapper' => 'content-type-select',
        'method' => 'replace',
        'effect' => 'fade',
      ],
      '#default_value' => $form_state
        ->getValue('entity_type'),
    ];
    $form['gathercontent']['content_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Drupal bundle type'),
      '#options' => $form_state
        ->getValue('entity_type') ? $this
        ->getBundles($form_state
        ->getValue('entity_type')) : [],
      '#required' => TRUE,
      '#wrapper_attributes' => [
        'class' => [
          'inline-label',
        ],
      ],
      '#prefix' => '<div id="content-type-select">',
      '#suffix' => '</div>',
    ];
    $form['gathercontent']['project_id'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Project ID'),
      '#options' => $this
        ->getProjects(),
      '#required' => TRUE,
      '#wrapper_attributes' => [
        'class' => [
          'inline-label',
        ],
      ],
      '#default_value' => $form_state
        ->getValue('project_id'),
    ];
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Create'),
      '#button_type' => 'primary',
      '#weight' => 10,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->mappingCreator
      ->generateMapping($form_state
      ->getValue('entity_type'), $form_state
      ->getValue('content_type'), $form_state
      ->getValue('project_id'));
    $this
      ->messenger()
      ->addMessage('Mapping created successfully');
  }

  /**
   * Get list of bundle types.
   *
   * @param string $entityType
   *   Entity type ID.
   *
   * @return array
   *   Assoc array of bundle types.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getBundles(string $entityType) : array {
    $mappingStorage = $this->entityTypeManager
      ->getStorage('gathercontent_mapping');
    $bundleTypes = $this->entityTypeBundleInfo
      ->getBundleInfo($entityType);
    $response = [];
    foreach ($bundleTypes as $key => $value) {
      $mapping = $mappingStorage
        ->loadByProperties([
        'entity_type' => $entityType,
        'content_type' => $key,
      ]);
      if ($mapping) {
        continue;
      }
      $response[$key] = $value['label'];
    }
    return $response;
  }

  /**
   * Get list of entity types.
   *
   * @return array
   *   Assoc array of entity types.
   */
  public function getEntityTypes() {
    $entityTypes = $this->entityTypeManager
      ->getDefinitions();
    $unsupportedTypes = [
      'user',
      'file',
      'menu_link_content',
    ];
    $response = [];
    foreach ($entityTypes as $key => $value) {
      if ($value) {
        $class = $value
          ->getOriginalClass();
        if (in_array(FieldableEntityInterface::class, class_implements($class)) && !in_array($key, $unsupportedTypes)) {
          $label = (string) $value
            ->getLabel();
          $response[$key] = $label;
        }
      }
    }
    return $response;
  }

  /**
   * Ajax callback for mapping multistep form.
   *
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *
   * @return array
   *   Array of form elements.
   */
  public function getContentTypes(array &$form, FormStateInterface $form_state) {
    $form_state
      ->setRebuild(TRUE);
    return $form['gathercontent']['content_type'];
  }

  /**
   * Returns all projects for given account.
   *
   * @return array
   */
  public function getProjects() {
    return $this->mappingCreator
      ->getProjects();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CreateTemplateForm::$entityTypeBundleInfo protected property The entity bundle info.
CreateTemplateForm::$entityTypeManager protected property Entity type manager.
CreateTemplateForm::$mappingCreator protected property Mapping creator.
CreateTemplateForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
CreateTemplateForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
CreateTemplateForm::getBundles public function Get list of bundle types.
CreateTemplateForm::getContentTypes public function Ajax callback for mapping multistep form.
CreateTemplateForm::getEntityTypes public function Get list of entity types.
CreateTemplateForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
CreateTemplateForm::getProjects public function Returns all projects for given account.
CreateTemplateForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
CreateTemplateForm::__construct public function MappingCreator constructor.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.