You are here

class MentionsTypeForm in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm
  2. 8 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm
  3. 8.2 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm
  4. 8.3 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm
  5. 8.4 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm
  6. 8.5 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm
  7. 8.6 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm
  8. 8.7 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm
  9. 8.8 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm
  10. 10.0.x modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm
  11. 10.1.x modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm
  12. 10.2.x modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm

Class MentionsTypeForm.

@package Drupal\mentiona\Form

Hierarchy

Expanded class hierarchy of MentionsTypeForm

2 string references to 'MentionsTypeForm'
mentions.routing.yml in modules/custom/mentions/mentions.routing.yml
modules/custom/mentions/mentions.routing.yml
mentions.routing.yml in modules/custom/mentions/mentions.routing.yml
modules/custom/mentions/mentions.routing.yml

File

modules/custom/mentions/src/Form/MentionsTypeForm.php, line 21

Namespace

Drupal\mentions\Form
View source
class MentionsTypeForm extends EntityForm implements ContainerInjectionInterface {
  use ConfigFormBaseTrait;

  /**
   * The mentions plugin manager.
   *
   * @var \Drupal\mentions\MentionsPluginManager
   */
  protected $mentionsManager;

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

  /**
   * The entity type repository service.
   *
   * @var \Drupal\Core\Entity\EntityTypeRepositoryInterface
   */
  protected $entityTypeRepository;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public function __construct(MentionsPluginManager $mentions_manager, EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, ModuleHandlerInterface $module_handler) {
    $this->mentionsManager = $mentions_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->entityTypeRepository = $entity_type_repository;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.mentions'), $container
      ->get('entity_type.manager'), $container
      ->get('entity_type.repository'), $container
      ->get('module_handler'));
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $plugin_names = $this->mentionsManager
      ->getPluginNames();
    $entity = $this->entity;
    $inputsettings = $entity
      ->get('input');
    $entity_id = isset($entity) ? $entity
      ->id() : '';
    $all_entitytypes = array_keys($this->entityTypeRepository
      ->getEntityTypeLabels());
    $candidate_entitytypes = [];
    foreach ($all_entitytypes as $entity_type) {
      $entitytype_info = $this->entityTypeManager
        ->getDefinition($entity_type);
      $configentityclassname = ContentEntityType::class;
      $entitytype_type = get_class($entitytype_info);
      if ($entitytype_type == $configentityclassname) {
        $candidate_entitytypes[$entity_type] = $entitytype_info
          ->getLabel()
          ->getUntranslatedString();
        $candidate_entitytypefields[$entity_type][$entitytype_info
          ->getKey('id')] = $entitytype_info
          ->getKey('id');
        if ($entity_type === 'user') {
          $candidate_entitytypefields[$entity_type]['name'] = 'name';
        }
        else {
          $candidate_entitytypefields[$entity_type][$entitytype_info
            ->getKey('label')] = $entitytype_info
            ->getKey('label');
        }
      }
    }
    $config = $this
      ->config('mentions.mentions_type.' . $entity_id);
    $form['name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Name'),
      '#required' => TRUE,
      '#description' => $this
        ->t('The human-readable name of this mention type. It is recommended that this name begin with a capital letter and contain only letters, numbers, and spaces. This name must be unique.'),
      '#default_value' => $config
        ->get('name'),
    ];
    $form['mention_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Mention Type'),
      '#options' => $plugin_names,
      '#default_value' => $config
        ->get('mention_type'),
    ];
    $form['description'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Description'),
      '#description' => $this
        ->t('Describe this mention type.'),
      '#default_value' => $config
        ->get('description'),
    ];
    $form['input'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Input Settings'),
      '#tree' => TRUE,
    ];
    $form['input']['prefix'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Prefix'),
      '#default_value' => $config
        ->get('input.prefix'),
      '#size' => 2,
    ];
    $form['input']['suffix'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Suffix'),
      '#default_value' => $config
        ->get('input.suffix'),
      '#size' => 2,
    ];
    $entitytype_selection = $config
      ->get('input.entity_type');
    $form['input']['entity_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Entity Type'),
      '#options' => $candidate_entitytypes,
      '#default_value' => $entitytype_selection,
      '#ajax' => [
        'callback' => [
          $this,
          'changeEntityTypeInForm',
        ],
        'wrapper' => 'edit-input-value-wrapper',
        'event' => 'change',
        'progress' => [
          'type' => 'throbber',
          'message' => $this
            ->t('Please wait...'),
        ],
      ],
    ];
    if (!isset($candidate_entitytypefields)) {
      $inputvalue_options = [];
    }
    elseif (isset($entitytype_selection)) {
      $inputvalue_options = $candidate_entitytypefields[$entitytype_selection];
    }
    else {
      $inputvalue_options = array_values($candidate_entitytypefields)[0];
    }
    $inputvalue_default_value = count($inputsettings) == 0 ? 0 : $inputsettings['inputvalue'];
    $form['input']['inputvalue'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Value'),
      '#options' => $inputvalue_options,
      '#default_value' => $inputvalue_default_value,
      '#prefix' => '<div id="edit-input-value-wrapper">',
      '#suffix ' => '</div>',
      '#validated' => 1,
    ];
    $form['output'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Output Settings'),
      '#tree' => TRUE,
    ];
    $form['output']['outputvalue'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Value'),
      '#description' => $this
        ->t('This field supports tokens.'),
      '#default_value' => $config
        ->get('output.outputvalue'),
    ];
    $form['output']['renderlink'] = [
      '#type' => 'checkbox',
      '#title' => 'Render as link',
      '#default_value' => $config
        ->get('output.renderlink'),
    ];
    $form['output']['renderlinktextbox'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Link'),
      '#description' => $this
        ->t('This field supports tokens.'),
      '#default_value' => $config
        ->get('output.renderlinktextbox'),
      '#states' => [
        'visible' => [
          ':input[name="output[renderlink]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    if ($this->moduleHandler
      ->moduleExists('token')) {
      $form['output']['tokens'] = [
        '#theme' => 'token_tree_link',
        '#token_types' => 'all',
        '#show_restricted' => TRUE,
        '#theme_wrappers' => [
          'form_element',
        ],
      ];
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    $actions['submit']['#value'] = $this
      ->t('Save Mentions Type');
    return $actions;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $form_state
      ->setRedirect('entity.mentions_type.list');
  }

  /**
   * {@inheritdoc}
   */
  public function changeEntityTypeInForm(array &$form, FormStateInterface $form_state) {
    $entitytype_state = $form_state
      ->getValue([
      'input',
      'entity_type',
    ]);
    $entitytype_info = $this->entityTypeManager
      ->getDefinition($entitytype_state);
    $id = $entitytype_info
      ->getKey('id');
    $label = $entitytype_info
      ->getKey('label');
    if ($entitytype_state == 'user') {
      $label = 'name';
    }
    unset($form['input']['inputvalue']['#options']);
    unset($form['input']['inputvalue']['#default_value']);
    $form['input']['inputvalue']['#options'][$id] = $id;
    $form['input']['inputvalue']['#options'][$label] = $label;
    $form['input']['inputvalue']['#default_value'] = $id;
    return $form['input']['inputvalue'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entity protected property The entity being used by this form. 11
EntityForm::$operation protected property The name of the current operation.
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity 3
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties. 9
EntityForm::form public function Gets the actual form array to be built. 36
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 6
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 3
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::save public function Form submission handler for the 'save' action. Overrides EntityFormInterface::save 47
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 3
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.
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 72
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.
MentionsTypeForm::$entityTypeManager protected property The entity type manager service. Overrides EntityForm::$entityTypeManager
MentionsTypeForm::$entityTypeRepository protected property The entity type repository service.
MentionsTypeForm::$mentionsManager protected property The mentions plugin manager.
MentionsTypeForm::$moduleHandler protected property The module handler service. Overrides EntityForm::$moduleHandler
MentionsTypeForm::actions protected function Returns an array of supported actions for the current entity form. Overrides EntityForm::actions
MentionsTypeForm::buildForm public function Form constructor. Overrides EntityForm::buildForm
MentionsTypeForm::changeEntityTypeInForm public function
MentionsTypeForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
MentionsTypeForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
MentionsTypeForm::getFormId public function Returns a unique string identifying the form. Overrides EntityForm::getFormId
MentionsTypeForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides EntityForm::submitForm
MentionsTypeForm::__construct public function
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
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. 4
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.