You are here

public function FillPdfLinkManipulator::parseLink in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 src/Service/FillPdfLinkManipulator.php \Drupal\fillpdf\Service\FillPdfLinkManipulator::parseLink()

Parses a Url object.

Parameters

\Drupal\Core\Url $link: The valid URL containing the FillPDF generation metadata. e.g. 'http://example.com/fillpdf?entity_ids[]=node:1&entity_ids[]=contact:7'.

Return value

array An associative array representing the request context and containing the following properties: fid: int ID of the FillPDF form. sample: true|null Flag indicating if a sample file is to be generated. TRUE if yes, otherwise NULL. entity_ids: string[] Array of entity_type:entity_id pairs to populate the fields with. Will otherwise contain an empty array. force_download: true|null Flag indicating if the populated file should always be downloaded. TRUE if yes, otherwise NULL. flatten: false|null Flag indicating if the populated file should be flattened. FALSE if not, otherwise NULL.

Throws

\InvalidArgumentException If $link contains no query string or doesn't specify a valid FillPdfForm.

Overrides FillPdfLinkManipulatorInterface::parseLink

2 calls to FillPdfLinkManipulator::parseLink()
FillPdfLinkManipulator::parseRequest in src/Service/FillPdfLinkManipulator.php
@todo: Maybe this should return a FillPdfLinkContext object or something? Guess it depends on how much I end up needing to change it.
FillPdfLinkManipulator::parseUrlString in src/Service/FillPdfLinkManipulator.php
Parses a root-relative URL.

File

src/Service/FillPdfLinkManipulator.php, line 61

Class

FillPdfLinkManipulator

Namespace

Drupal\fillpdf\Service

Code

public function parseLink(Url $link) {
  $query = $link
    ->getOption('query');
  if (empty($query['fid'])) {
    throw new \InvalidArgumentException('No FillPDF Form was specified in the query string, so failing.');
  }
  $fillpdf_form = FillPdfForm::load($query['fid']);
  if (!$fillpdf_form) {
    throw new \InvalidArgumentException("The requested FillPDF Form doesn't exist, so failing.");
  }

  // Set the fid, merging in evaluated boolean flags.
  $context = [
    'fid' => $query['fid'],
  ] + static::parseBooleanFlags($query);

  // Early return if PDF is just to be populated with sample data.
  if ($context['sample'] === TRUE) {
    $context['entity_ids'] = [];
    return $context;
  }

  // No sample and no entities given, so try enriching with defaults.
  if (empty($query['entity_id']) && empty($query['entity_ids'])) {
    $default_entity_id = $fillpdf_form->default_entity_id->value;
    if (!empty($default_entity_id)) {
      $query['entity_id'] = $default_entity_id;
      $query['entity_type'] = $fillpdf_form->default_entity_type->value;
    }
  }

  // Merge in parsed entities.
  $context += static::parseEntityIds($query);
  return $context;
}