writeData example

$settings = $field_definition->getSettings();

    // Prepare destination.     $dirname = static::doGetUploadLocation($settings);
    \Drupal::service('file_system')->prepareDirectory($dirname, FileSystemInterface::CREATE_DIRECTORY);

    // Generate a file entity.     $destination = $dirname . '/' . $random->name(10, TRUE) . '.txt';
    $data = $random->paragraphs(3);
    /** @var \Drupal\file\FileRepositoryInterface $file_repository */
    $file_repository = \Drupal::service('file.repository');
    $file = $file_repository->writeData($data$destination, FileSystemInterface::EXISTS_ERROR);
    $values = [
      'target_id' => $file->id(),
      'display' => (int) $settings['display_default'],
      'description' => $random->sentences(10),
    ];
    return $values;
  }

  /** * Determines whether an item should be displayed when rendering the field. * * @return bool * TRUE if the item should be displayed, FALSE if not. */

  protected function createTemporaryFile($data, UserInterface $user = NULL) {
    /** @var \Drupal\file\FileRepositoryInterface $file_repository */
    $file_repository = \Drupal::service('file.repository');
    $file = $file_repository->writeData($data, "public://");

    if ($file) {
      if ($user) {
        $file->setOwner($user);
      }
      else {
        $file->setOwner($this->adminUser);
      }
      // Change the file status to be temporary.       $file->setTemporary();
      // Save the changes.

  public function createTempFiles() {
    /** @var \Drupal\file\FileRepositoryInterface $fileRepository */
    $fileRepository = \Drupal::service('file.repository');

    // Temporary file that is old.     $destination = "public://";
    $temp_old = $fileRepository->writeData('', $destination);
    $connection = Database::getConnection();
    $connection->update('file_managed')
      ->fields([
        'status' => 0,
        'changed' => REQUEST_TIME - $this->config('system.file')->get('temporary_maximum_age') - 1,
      ])
      ->condition('fid', $temp_old->id())
      ->execute();
    $this->assertFileExists($temp_old->getFileUri());

    // Temporary file that is new.
/** * JSON descriptor. * * @author Jean-François Simon <contact@jfsimon.fr> * * @internal */
class JsonDescriptor extends Descriptor
{
    protected function describeInputArgument(InputArgument $argument, array $options = []): void
    {
        $this->writeData($this->getInputArgumentData($argument)$options);
    }

    protected function describeInputOption(InputOption $option, array $options = []): void
    {
        $this->writeData($this->getInputOptionData($option)$options);
        if ($option->isNegatable()) {
            $this->writeData($this->getInputOptionData($option, true)$options);
        }
    }

    protected function describeInputDefinition(InputDefinition $definition, array $options = []): void
    {
class JsonDescriptor extends Descriptor
{
    protected function describeDefaults(array $options): void
    {
        $data['builtin_form_types'] = $options['core_types'];
        $data['service_form_types'] = $options['service_types'];
        if (!$options['show_deprecated']) {
            $data['type_extensions'] = $options['extensions'];
            $data['type_guessers'] = $options['guessers'];
        }

        $this->writeData($data$options);
    }

    protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedFormType, array $options = []): void
    {
        $this->collectOptions($resolvedFormType);

        if ($options['show_deprecated']) {
            $this->filterOptionsByDeprecated($resolvedFormType);
        }

        $formOptions = [
            

class JsonDescriptor extends Descriptor
{
    protected function describeRouteCollection(RouteCollection $routes, array $options = []): void
    {
        $data = [];
        foreach ($routes->all() as $name => $route) {
            $data[$name] = $this->getRouteData($route);
        }

        $this->writeData($data$options);
    }

    protected function describeRoute(Route $route, array $options = []): void
    {
        $this->writeData($this->getRouteData($route)$options);
    }

    protected function describeContainerParameters(ParameterBag $parameters, array $options = []): void
    {
        $this->writeData($this->sortParameters($parameters)$options);
    }

    

  public function testWithFilename() {
    $contents = $this->randomMachineName();

    // Using filename with non-latin characters.     // cSpell:disable-next-line     $filename = 'Текстовый файл.txt';

    $result = $this->fileRepository->writeData($contents, 'public://' . $filename);
    $this->assertNotFalse($result, 'Unnamed file saved correctly.');

    $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
    assert($stream_wrapper_manager instanceof StreamWrapperManagerInterface);
    $this->assertEquals('public', $stream_wrapper_manager::getScheme($result->getFileUri()), "File was placed in Drupal's files directory.");
    $this->assertEquals($filename, \Drupal::service('file_system')->basename($result->getFileUri()), 'File was named correctly.');
    $this->assertEquals($contentsfile_get_contents($result->getFileUri()), 'Contents of the file are correct.');
    $this->assertEquals('text/plain', $result->getMimeType(), 'A MIME type was set.');
    $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");

    // Check that the correct hooks were called.
/** * JSON descriptor. * * @author Jean-François Simon <contact@jfsimon.fr> * * @internal */
class JsonDescriptor extends Descriptor
{
    protected function describeInputArgument(InputArgument $argument, array $options = []): void
    {
        $this->writeData($this->getInputArgumentData($argument)$options);
    }

    protected function describeInputOption(InputOption $option, array $options = []): void
    {
        $this->writeData($this->getInputOptionData($option)$options);
        if ($option->isNegatable()) {
            $this->writeData($this->getInputOptionData($option, true)$options);
        }
    }

    protected function describeInputDefinition(InputDefinition $definition, array $options = []): void
    {
Home | Imprint | This part of the site doesn't use cookies.