/** * @param \Drupal\file\FileInterface $file * @param \Drupal\fillpdf\FillPdfFormInterface $existing_form * @return array */ public function attachPdfToForm(FileInterface $file, FillPdfFormInterface $existing_form = NULL) { $this->saveFileUpload($file); if ($existing_form) { $fillpdf_form = $existing_form; $fillpdf_form->file = $file; } else { $fillpdf_form = FillPdfForm::create([ 'file' => $file, 'title' => $file->filename, 'scheme' => $this->config('fillpdf.settings')->get('scheme'), ]); } // Save PDF configuration before parsing. $fillpdf_form->save(); $config = $this->config('fillpdf.settings'); $fillpdf_service = $config->get('backend'); /** @var FillPdfBackendPluginInterface $backend */ $backend = $this->backendManager->createInstance($fillpdf_service, $config->get()); // Attempt to parse the fields in the PDF. $fields = $backend->parse($fillpdf_form); $form_fields = []; foreach ((array) $fields as $arr) { if ($arr['type']) { // Don't store "container" fields // pdftk sometimes inserts random � markers - strip these out. // NOTE: This may break forms that actually DO contain this pattern, // but 99%-of-the-time functionality is better than merge failing due // to improper parsing. $arr['name'] = str_replace('�', '', $arr['name']); $field = FillPdfFormField::create( [ 'fillpdf_form' => $fillpdf_form, 'pdf_key' => $arr['name'], 'value' => '', ] ); $form_fields[] = $field; } } // Save the fields that were parsed out (if any). If none were, set a // warning message telling the user that. foreach ($form_fields as $fillpdf_form_field) { /** @var FillPdfFormField $fillpdf_form_field */ $fillpdf_form_field->save(); } return ['form' => $fillpdf_form, 'fields' => $form_fields]; }
/** * {@inheritDoc} */ public function parseLink(Url $link) { $query = $link->getOption('query'); if (!$query) { throw new \InvalidArgumentException('The \Drupal\Core\Url you pass in must have its \'query\' option set.'); } $request_context = [ 'entity_ids' => NULL, 'fid' => NULL, 'sample' => NULL, 'force_download' => FALSE, 'flatten' => TRUE, ]; if (!empty($query['sample'])) { $sample = TRUE; } // Is this just the PDF populated with sample data? $request_context['sample'] = $sample; if (!empty($query['fid'])) { $request_context['fid'] = $query['fid']; } else { throw new \InvalidArgumentException('fid parameter missing from query string; cannot determine how to proceed, so failing.'); } if (!empty($query['entity_type'])) { $request_context['entity_type'] = $query['entity_type']; } $request_context['entity_ids'] = $entity_ids = []; if (!empty($query['entity_id']) || !empty($query['entity_ids'])) { $entity_ids = (!empty($query['entity_id']) ? [$query['entity_id']] : $query['entity_ids']); // Re-key entity IDs so they can be loaded easily with loadMultiple(). // If we have type information, add it to the types array, and remove it // in order to make sure we only store the ID in the entity_ids key. foreach ($entity_ids as $entity_id) { $entity_id_parts = explode(':', $entity_id); if (count($entity_id_parts) == 2) { $entity_type = $entity_id_parts[0]; $entity_id = $entity_id_parts[1]; } elseif (!empty($request_context['entity_type'])) { $entity_type = $request_context['entity_type']; } else { $entity_type = 'node'; } $request_context['entity_ids'] += [ $entity_type => [], ]; $request_context['entity_ids'][$entity_type][$entity_id] = $entity_id; } } else { // Populate defaults. $fillpdf_form = FillPdfForm::load($request_context['fid']); $default_entity_id = $fillpdf_form->default_entity_id->value; if ($default_entity_id) { $default_entity_type = $fillpdf_form->default_entity_type->value; if (empty($default_entity_type)) { $default_entity_type = 'node'; } $request_context['entity_ids'] = [ $default_entity_type => [$default_entity_id => $default_entity_id], ]; } } // We've processed the shorthand forms, so unset them. unset($request_context['entity_id'], $request_context['entity_type']); if (!$query['download'] && (int) $query['download'] == 1) { $request_context['force_download'] = TRUE; } if ($query['flatten'] && (int) $query['flatten'] == 0) { $request_context['flatten'] = FALSE; } return $request_context; }