o365-2.0.3/modules/o365_onedrive/src/Plugin/Block/SharedFilesBlock.php

modules/o365_onedrive/src/Plugin/Block/SharedFilesBlock.php
<?php

namespace Drupal\o365_onedrive\Plugin\Block;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\o365\Block\O365UncachedBlockBase;
use Drupal\o365\GraphService;
use Drupal\o365_onedrive\GetFilesAndFoldersServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a 'Shared Files' block.
 *
 * @Block(
 *   id = "o365_shared_files",
 *   admin_label = @Translation("Shared Files"),
 *   category = @Translation("Microsoft 365")
 * )
 */
final class SharedFilesBlock extends O365UncachedBlockBase implements ContainerFactoryPluginInterface {

  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    protected GraphService $graphService,
    protected GetFilesAndFoldersServiceInterface $getFilesAndFoldersService,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $graphService);
  }

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

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Core\TempStore\TempStoreException
   * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException
   * @throws \Microsoft\Graph\Exception\GraphException
   */
  public function build(): array {
    $config = $this->getConfiguration();
    $numFiles = ($config['num_shared_files']) ?? 10;
    $blockData = $this->getFilesAndFoldersService->listSharedFilesAndFolders($numFiles);

    if (!empty($blockData)) {
      return $blockData;
    }

    if ($config['show_shared_files_block']) {
      return [
        '#theme' => 'o365_onedrive_results',
        '#results' => $config['show_shared_files_block'],
      ];
    }

    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state): array {
    $form = parent::blockForm($form, $form_state);
    $config = $this->getConfiguration();

    $form['show_shared_files_block'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Show shared files when there is no data'),
      '#default_value' => ($config['show_shared_files_block']) ?? FALSE,
    ];

    $form['num_shared_files'] = [
      '#type' => 'number',
      '#title' => $this->t('The number of files to show'),
      '#default_value' => ($config['num_shared_files']) ?? 10,
    ];

    $form['shared_files_block_text'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Shared files block text'),
      '#description' => $this->t('Enter the text you would like to show'),
      '#placeholder' => $this->t('Enter the text you would like to show'),
      '#default_value' => $config['shared_files_block_text'] ?? $this->t('No shared files found.'),
      '#states' => [
        'visible' => [
          ':input[name="settings[show_shared_files_block]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state): void {
    $values = $form_state->getValues();
    $this->configuration['show_shared_files_block'] = $values['show_shared_files_block'];
    $this->configuration['shared_files_block_text'] = $values['shared_files_block_text'];
    $this->configuration['num_shared_files'] = $values['num_shared_files'];
  }

}

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

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