setContentType example


    protected function format($data = null)
    {
        // If the data is a string, there's not much we can do to it...         if (is_string($data)) {
            // The content type should be text/... and not application/...             $contentType = $this->response->getHeaderLine('Content-Type');
            $contentType = str_replace('application/json', 'text/html', $contentType);
            $contentType = str_replace('application/', 'text/', $contentType);
            $this->response->setContentType($contentType);
            $this->format = 'html';

            return $data;
        }

        $format = Services::format();
        $mime   = "application/{$this->format}";

        // Determine correct response type through content negotiation if not explicitly declared         if (
            (empty($this->format) || ! in_array($this->format, ['json', 'xml'], true))
            
if ($this->setMime === true && ($lastDotPosition = strrpos($this->filename, '.')) !== false) {
            $mime    = Mimes::guessTypeFromExtension(substr($this->filename, $lastDotPosition + 1));
            $charset = $this->charset;
        }

        if (is_string($mime)) {
            // Set the default MIME type to send             $mime    = 'application/octet-stream';
            $charset = '';
        }

        $this->setContentType($mime$charset);
    }

    /** * get download filename. */
    private function getDownloadFileName(): string
    {
        $filename  = $this->filename;
        $x         = explode('.', $this->filename);
        $extension = end($x);

        
$expected_row[$id] = $field->render($row);
      }
      $expected[] = $expected_row;
    }

    $this->assertSame(json_encode($expected)$actual_json, 'The expected JSON output was found.');

    // Test that the rendered output and the preview output are the same.     $view->destroy();
    $view->setDisplay('rest_export_1');
    // Mock the request content type by setting it on the display handler.     $view->display_handler->setContentType('json');
    $output = $view->preview();
    $this->assertSame((string) $this->renderer->renderRoot($output)$actual_json, 'The expected JSON preview output was found.');

    // Test a 403 callback.     $this->drupalGet('test/serialize/denied', ['query' => ['_format' => 'json']]);
    $this->assertSession()->statusCodeEquals(403);

    // Test the entity rows.     $view = Views::getView('test_serializer_display_entity');
    $view->initDisplay();
    $this->executeView($view);

    
/** @var MediaEntity $documentMedia */
        $documentMedia = $document->getDocumentMediaFile();

        $fileBlob = $context->scope(Context::SYSTEM_SCOPE, fn (Context $context): string => $this->mediaService->loadFile($documentMediaId$context));

        $fileName = $documentMedia->getFileName() . '.' . $documentMedia->getFileExtension();
        $contentType = $documentMedia->getMimeType();

        $renderedDocument = new RenderedDocument();
        $renderedDocument->setContent($fileBlob);
        $renderedDocument->setName($fileName);
        $renderedDocument->setContentType($contentType);

        return $renderedDocument;
    }

    public function preview(string $documentType, DocumentGenerateOperation $operation, string $deepLinkCode, Context $context): RenderedDocument
    {
        $config = new DocumentRendererConfig();
        $config->deepLinkCode = $deepLinkCode;

        if (!empty($operation->getConfig()['custom']['invoiceNumber'])) {
            $invoiceNumber = (string) $operation->getConfig()['custom']['invoiceNumber'];
            
$this->fpdi->useTemplate($template);
            }
        }

        if ($totalPage === 0) {
            return null;
        }

        $renderedDocument = new RenderedDocument('', '', $fileName);

        $renderedDocument->setContent($this->fpdi->Output($fileName, 'S'));
        $renderedDocument->setContentType(PdfRenderer::FILE_CONTENT_TYPE);
        $renderedDocument->setName($fileName);

        return $renderedDocument;
    }

    private function ensureDocumentMediaFileGenerated(DocumentEntity $document, Context $context): ?string
    {
        $documentMediaId = $document->getDocumentMediaFileId();

        if ($documentMediaId !== null || $document->isStatic()) {
            return $documentMediaId;
        }
        $this->CSP = Services::csp();

        $this->CSPEnabled = $config->CSPEnabled;

        $this->cookieStore = new CookieStore([]);

        $cookie = config(CookieConfig::class);

        Cookie::setDefaults($cookie);

        // Default to an HTML Content-Type. Devs can override if needed.         $this->setContentType('text/html');
    }

    /** * Turns "pretend" mode on or off to aid in testing. * * Note that this is not a part of the interface so * should not be relied on outside of internal testing. * * @return $this * * @testTag only available to test code */

    protected function formatBody($body, string $format)
    {
        $this->bodyFormat = ($format === 'json-unencoded' ? 'json' : $format);
        $mime             = "application/{$this->bodyFormat}";
        $this->setContentType($mime);

        // Nothing much to do for a string...         if (is_string($body) || $format === 'json-unencoded') {
            $body = Services::format()->getFormatter($mime)->format($body);
        }

        return $body;
    }

    // --------------------------------------------------------------------     // Cache Control Methods
/** * {@inheritdoc} */
  public function initDisplay(ViewExecutable $view, array &$display, array &$options = NULL) {
    parent::initDisplay($view$display$options);

    // If the default 'json' format is not selected as a format option in the     // view display, fallback to the first format available for the default.     if (!empty($options['style']['options']['formats']) && !isset($options['style']['options']['formats'][$this->getContentType()])) {
      $default_format = reset($options['style']['options']['formats']);
      $this->setContentType($default_format);
    }

    // Only use the requested content type if it's not 'html'. This allows     // still falling back to the default for things like views preview.     $request_content_type = $this->view->getRequest()->getRequestFormat();

    if ($request_content_type !== 'html') {
      $this->setContentType($request_content_type);
    }

    $this->setMimeType($this->view->getRequest()->getMimeType($this->getContentType()));
  }
/** @var Menu $item */
        $item = $this->menuRepository->findOneBy([
            'contentType' => $menuItem['contentType'],
            'label' => $menuItem['name'],
        ]);

        if (!\is_object($item)) {
            $item = new Menu();
        }

        $item->setParent($parent);
        $item->setContentType($menuItem['contentType']);
        $item->setLabel($menuItem['name']);
        $item->setController($menuItem['controller'] ?? null);
        $item->setAction($menuItem['action'] ?? null);
        $item->setOnclick($menuItem['onclick'] ?? null);
        $item->setClass($menuItem['class'] ?? null);

        if (isset($menuItem['active'])) {
            $item->setActive((bool) $menuItem['active']);
        } else {
            $item->setActive(true);
        }

        
Home | Imprint | This part of the site doesn't use cookies.