You are here

class VimeoSettingsForm in Vimeo Video Uploader 8

Same name and namespace in other branches
  1. 8.4 src/Form/VimeoSettingsForm.php \Drupal\vimeo_video_uploader\Form\VimeoSettingsForm

Provide configuration form for user to provide vimeo API information for a

Hierarchy

Expanded class hierarchy of VimeoSettingsForm

1 string reference to 'VimeoSettingsForm'
vimeo_video_uploader.routing.yml in ./vimeo_video_uploader.routing.yml
vimeo_video_uploader.routing.yml

File

src/Form/VimeoSettingsForm.php, line 19
Contains \Drupal\vimeo_video_uploader\Form\SettingsForm.

Namespace

Drupal\vimeo_video_uploader\Form
View source
class VimeoSettingsForm extends ConfigFormBase {

  /**
   *
   */
  public function __construct(ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory) {
    $this
      ->setConfigFactory($config_factory);
    $this->logger = $logger_factory
      ->get('vimeo_video_uploader');
  }

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

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

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

    // Get the configuration from ConfigFormBase::config().
    $config = self::config('vimeo_video_uploader.settings');
    $form['values'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('VIMEO VIDEO UPLOAD CONFIGURATION'),
      '#collapsible' => TRUE,
      '#tree' => TRUE,
    );
    $form['values']['client_id'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Enter (Vimeo Client Identifier) '),
      '#default_value' => $config
        ->get('values.client_id'),
      '#required' => TRUE,
    );
    $form['values']['client_secret'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Enter (Vimeo Client Secrets)'),
      '#default_value' => $config
        ->get('values.client_secret'),
      '#required' => TRUE,
    );
    $form['values']['access_token'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Enter generated (Your new Access token)'),
      '#default_value' => $config
        ->get('values.access_token'),
      '#required' => TRUE,
    );
    $form['values']['content_type_select'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Select the Content Types from which you have to upload video to Vimeo'),
      '#options' => $this
        ->getContentTypeList(),
      '#default_value' => $config
        ->get('values.content_type_select'),
      '#required' => TRUE,
    );
    return parent::buildForm($form, $form_state);
  }
  public function getContentTypeList() {
    $contentTypes = \Drupal::service('entity.manager')
      ->getStorage('node_type')
      ->loadMultiple();
    $contentTypesList = [];
    foreach ($contentTypes as $contentType) {
      $contentTypesList[$contentType
        ->id()] = $contentType
        ->label();
    }
    return $contentTypesList;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Set configuration.
    $config = self::config('vimeo_video_uploader.settings');
    $form_state_values = $form_state
      ->getValues();
    $message = "Saved the Vimeo configuration.";
    $old_content_type_select = $config
      ->get('values.content_type_select');
    if ($old_content_type_select !== $form_state_values['values']['content_type_select']) {

      //delete some things
      $entityManager = \Drupal::service('entity.manager');
      $fields = $entityManager
        ->getFieldDefinitions('node', $old_content_type_select);
      if (isset($fields['field_vimeo_file_browse']) && ($field = $fields['field_vimeo_file_browse'])) {
        $field
          ->delete();
      }
      if (isset($fields['field_embeddedvideo']) && ($field1 = $fields['field_embeddedvideo'])) {
        $field1
          ->delete();
      }
      $this
        ->AddContentTypeField($form_state_values['values']['content_type_select']);
      $message = "Created 'Browse video for uploading to Vimeo' field in '" . strtoupper($form_state_values['values']['content_type_select']) . "' Content type.";
    }
    $config
      ->set('values.client_id', $form_state_values['values']['client_id'])
      ->set('values.client_secret', $form_state_values['values']['client_secret'])
      ->set('values.access_token', $form_state_values['values']['access_token'])
      ->set('values.content_type_select', $form_state_values['values']['content_type_select']);
    $config
      ->save();
    drupal_set_message($message, 'status');
  }
  function AddContentTypeField($bundle) {
    \Drupal\field\Entity\FieldStorageConfig::create(array(
      'field_name' => 'field_vimeo_file_browse',
      'entity_type' => 'node',
      'type' => 'file',
    ))
      ->save();
    \Drupal\field\Entity\FieldConfig::create([
      'field_name' => 'field_vimeo_file_browse',
      'entity_type' => 'node',
      'bundle' => $bundle,
      'settings' => array(
        'file_extensions' => 'mp4',
      ),
      'label' => 'Browse video for uploading to Vimeo',
    ])
      ->save();
    entity_get_form_display('node', $bundle, 'default')
      ->setComponent('field_vimeo_file_browse', array(
      'type' => 'file_generic',
    ))
      ->save();
    entity_get_display('node', $bundle, 'default')
      ->setComponent('field_vimeo_file_browse', array(
      'type' => 'file_default',
    ))
      ->save();

    //add embedded video input field
    \Drupal\field\Entity\FieldStorageConfig::create(array(
      'field_name' => 'field_embeddedvideo',
      'entity_type' => 'node',
      'type' => 'video_embed_field',
    ))
      ->save();
    \Drupal\field\Entity\FieldConfig::create([
      'field_name' => 'field_embeddedvideo',
      'entity_type' => 'node',
      'bundle' => $bundle,
      'label' => 'Vimeo video link',
    ])
      ->save();
    entity_get_form_display('node', $bundle, 'default')
      ->setComponent('field_embeddedvideo', array(
      'type' => 'video_embed_field_textfield',
      'class' => 'neera',
    ))
      ->save();
    entity_get_display('node', $bundle, 'default')
      ->setComponent('field_embeddedvideo', array(
      'type' => 'video_embed_field_video',
    ))
      ->save();
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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::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.
VimeoSettingsForm::AddContentTypeField function
VimeoSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
VimeoSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
VimeoSettingsForm::getContentTypeList public function
VimeoSettingsForm::getEditableConfigNames public function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
VimeoSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
VimeoSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
VimeoSettingsForm::__construct public function Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase::__construct