You are here

class File in Commerce File 8.2

The file license type.

Plugin annotation


@CommerceLicenseType(
  id = "commerce_file",
  label = @Translation("File"),
)

Hierarchy

Expanded class hierarchy of File

3 string references to 'File'
commerce_file.links.menu.yml in ./commerce_file.links.menu.yml
commerce_file.links.menu.yml
commerce_file.links.task.yml in ./commerce_file.links.task.yml
commerce_file.links.task.yml
commerce_file.routing.yml in ./commerce_file.routing.yml
commerce_file.routing.yml

File

src/Plugin/Commerce/LicenseType/File.php, line 21

Namespace

Drupal\commerce_file\Plugin\Commerce\LicenseType
View source
class File extends LicenseTypeBase implements ContainerFactoryPluginInterface {

  /**
   * The file download logger.
   *
   * @var \Drupal\commerce_file\DownloadLoggerInterface
   */
  protected $downloadLogger;

  /**
   * Constructs a new File object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\commerce_file\DownloadLoggerInterface $download_logger
   *   The download logger.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, DownloadLoggerInterface $download_logger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->downloadLogger = $download_logger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('commerce_file.download_logger'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'file_download_limit' => NULL,
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $checkbox_parents = array_merge($form['#parents'], [
      'limit',
    ]);
    $checkbox_path = array_shift($checkbox_parents);
    $checkbox_path .= '[' . implode('][', $checkbox_parents) . ']';
    $form['limit'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Limit the number of times a user can download a licensed file'),
      '#description' => $this
        ->t('The value specified here overrides the globally configured download limit (Enter 0 for no limit).'),
      '#default_value' => !empty($this->configuration['file_download_limit']),
    ];
    $form['file_download_limit'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Download limit'),
      '#title_display' => 'invisible',
      '#default_value' => $this->configuration['file_download_limit'] ?? 100,
      '#min' => 0,
      '#states' => [
        'visible' => [
          ':input[name="' . $checkbox_path . '"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValue($form['#parents']);
    $this->configuration = [];
    if (!empty($values['limit'])) {
      $this->configuration['file_download_limit'] = $values['file_download_limit'];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildLabel(LicenseInterface $license) {
    return $this
      ->t('License for file');
  }

  /**
   * {@inheritdoc}
   */
  public function grantLicense(LicenseInterface $license) {

    // Clear the download log in order to reset download limits.
    $this->downloadLogger
      ->clear($license);
  }

  /**
   * {@inheritdoc}
   */
  public function revokeLicense(LicenseInterface $license) {
  }

  /**
   * {@inheritdoc}
   */
  public function buildFieldDefinitions() {
    $fields = parent::buildFieldDefinitions();
    $fields['file_download_limit'] = BundleFieldDefinition::create('integer')
      ->setLabel($this
      ->t('Download limit'))
      ->setRequired(FALSE)
      ->setSetting('unsigned', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'commerce_file_download_limit',
    ]);
    return $fields;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
File::$downloadLogger protected property The file download logger.
File::buildConfigurationForm public function Form constructor. Overrides LicenseTypeBase::buildConfigurationForm
File::buildFieldDefinitions public function Builds the field definitions for entities of this bundle. Overrides LicenseTypeBase::buildFieldDefinitions
File::buildLabel public function Builds a label for the given license. Overrides LicenseTypeInterface::buildLabel
File::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
File::defaultConfiguration public function Gets default configuration for this plugin. Overrides LicenseTypeBase::defaultConfiguration
File::grantLicense public function Reacts to the license being activated. Overrides LicenseTypeInterface::grantLicense
File::revokeLicense public function Reacts to the license being revoked. Overrides LicenseTypeInterface::revokeLicense
File::submitConfigurationForm public function Form submission handler. Overrides LicenseTypeBase::submitConfigurationForm
File::__construct public function Constructs a new File object. Overrides LicenseTypeBase::__construct
LicenseTypeBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
LicenseTypeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
LicenseTypeBase::getLabel public function Gets the license type label. Overrides LicenseTypeInterface::getLabel
LicenseTypeBase::getWorkflowId public function Gets the workflow ID this this license type should use. Overrides LicenseTypeInterface::getWorkflowId
LicenseTypeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
LicenseTypeBase::setConfigurationValuesOnLicense public function Copy configuration values to a license entity. Overrides LicenseTypeInterface::setConfigurationValuesOnLicense
LicenseTypeBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
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.