You are here

public function AdminSettingsForm::buildForm in PDF using mPDF 8

Same name and namespace in other branches
  1. 8.2 src/Form/AdminSettingsForm.php \Drupal\pdf_using_mpdf\Form\AdminSettingsForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/AdminSettingsForm.php, line 59

Class

AdminSettingsForm
Admin settings form.

Namespace

Drupal\pdf_using_mpdf\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Get settings for this form.
  $settings = $this
    ->configFactory()
    ->getEditable('pdf_using_mpdf.settings')
    ->get('pdf_using_mpdf');
  $form['pdf'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Config'),
  ];
  $filename = $settings['pdf_filename'];
  $form['pdf']['pdf_filename'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('PDF Filename'),
    '#required' => TRUE,
    '#default_value' => isset($filename) && $filename != NULL ? $filename : '[site:name] - PDF',
  ];
  $form['pdf']['pdf_save_option'] = [
    '#type' => 'radios',
    '#title' => t('Open PDF File in'),
    '#options' => [
      $this
        ->t('Web Browser'),
      $this
        ->t('Save Dialog Box'),
      $this
        ->t('Save to Server'),
    ],
    '#default_value' => $settings['pdf_save_option'],
  ];

  // Document properties.
  $form['pdf']['property'] = [
    '#type' => 'details',
    '#open' => FALSE,
    '#title' => $this
      ->t('Document Properties'),
    '#description' => $this
      ->t('These optional properties can be seen when inspecting the document properties like in Adobe Reader.'),
  ];
  $form['pdf']['property']['pdf_set_title'] = [
    '#type' => 'textfield',
    '#size' => 35,
    '#title' => $this
      ->t('Title'),
    '#default_value' => $settings['pdf_set_title'],
    '#description' => $this
      ->t('Set the title for the document.'),
  ];
  $form['pdf']['property']['pdf_set_author'] = [
    '#type' => 'textfield',
    '#size' => 35,
    '#title' => $this
      ->t('Author'),
    '#default_value' => $settings['pdf_set_author'],
    '#description' => $this
      ->t('Set the Author for the document.'),
  ];
  $form['pdf']['property']['pdf_set_subject'] = [
    '#type' => 'textfield',
    '#size' => 35,
    '#title' => $this
      ->t('Subject'),
    '#default_value' => $settings['pdf_set_subject'],
    '#description' => $this
      ->t('Set Subject of PDF.'),
  ];
  $form['pdf']['property']['pdf_set_creator'] = [
    '#type' => 'textfield',
    '#size' => 35,
    '#title' => $this
      ->t('Creator'),
    '#default_value' => $settings['pdf_set_creator'],
    '#description' => $this
      ->t('Set the document Creator.'),
  ];

  // Page settings.
  $form['pdf']['page_setting'] = [
    '#type' => 'details',
    '#open' => FALSE,
    '#title' => $this
      ->t('PDF Page Setting'),
    '#description' => $this
      ->t('All margin values should be specified as LENGTH in millimetres.'),
  ];
  $form['pdf']['page_setting']['margin_top'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Top Margin'),
    '#default_value' => $settings['margin_top'],
  ];
  $form['pdf']['page_setting']['margin_right'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Right Margin'),
    '#default_value' => $settings['margin_right'],
  ];
  $form['pdf']['page_setting']['margin_bottom'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Bottom Margin'),
    '#default_value' => $settings['margin_bottom'],
  ];
  $form['pdf']['page_setting']['margin_left'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Left Margin'),
    '#default_value' => $settings['margin_left'],
  ];
  $form['pdf']['page_setting']['margin_header'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Header Margin'),
    '#default_value' => $settings['margin_header'],
  ];
  $form['pdf']['page_setting']['margin_footer'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Footer Margin'),
    '#default_value' => $settings['margin_footer'],
  ];
  $form['pdf']['page_setting']['pdf_font_size'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Font Size'),
    '#default_value' => $settings['pdf_font_size'],
  ];
  $form['pdf']['page_setting']['pdf_default_font'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Default Font Style'),
    '#description' => $this
      ->t('These are the default fonts provided by mPDF. This may not work for HTML content if your styles already have a <em>font-family</em> property.'),
    '#options' => $this
      ->getDefaultFonts(),
    '#default_value' => $settings['pdf_default_font'],
  ];
  $form['pdf']['page_setting']['pdf_page_size'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Page Size'),
    '#options' => $this
      ->getPageSizeOptions(),
    '#default_value' => $settings['pdf_page_size'],
  ];
  $form['pdf']['page_setting']['dpi'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Document DPI'),
    '#default_value' => $settings['dpi'],
  ];
  $form['pdf']['page_setting']['img_dpi'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Image DPI'),
    '#default_value' => $settings['img_dpi'],
  ];
  $form['pdf']['page_setting']['orientation'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Orientation'),
    '#default_value' => $settings['orientation'],
    '#options' => [
      'P' => $this
        ->t('Portrait'),
      'L' => $this
        ->t('Landscape'),
    ],
  ];

  // Watermark.
  $form['pdf']['watermark'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#description' => $this
      ->t('Display watermark on every page.'),
    '#title' => $this
      ->t('PDF Watermark Option'),
  ];
  $form['pdf']['watermark']['watermark_opacity'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Watermark Transparency'),
    '#options' => [
      '0.1' => '0.1',
      '0.2' => '0.2',
      '0.3' => '0.3',
      '0.4' => '0.4',
      '0.5' => '0.5',
      '0.6' => '0.6',
      '0.7' => '0.7',
      '0.8' => '0.8',
      '0.9' => '0.9',
      '1.0' => '1.0',
    ],
    '#default_value' => $settings['watermark_opacity'],
  ];
  $form['pdf']['watermark']['watermark_option'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Watermark Option'),
    '#options' => [
      $this
        ->t('Text'),
      $this
        ->t('Image'),
    ],
    '#default_value' => $settings['watermark_option'],
  ];
  $form['pdf']['watermark']['pdf_watermark_text'] = [
    '#type' => 'textfield',
    '#default_value' => $settings['pdf_watermark_text'],
    '#placeholder' => $this
      ->t('Watermark text'),
  ];
  $form['pdf']['watermark']['watermark_image'] = [
    '#type' => 'managed_file',
    '#default_value' => $settings['watermark_image'],
    '#upload_location' => 'public://pdf_using_mpdf',
    '#description' => $this
      ->t('Display watermark image'),
    '#upload_validators' => [
      'file_validate_extensions' => [
        'gif png jpg jpeg',
      ],
      'file_validate_size' => [
        1024 * 1024,
      ],
    ],
  ];

  // Header.
  $form['pdf']['head_foot'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('PDF Header & Footer Option'),
    '#description' => $this
      ->t('use {PAGENO} for page numbering or {DATE j-m-Y} for current date.'),
  ];
  $form['pdf']['head_foot']['pdf_header'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Header content'),
    '#description' => $this
      ->t('Use a valid HTML code to write a custom header content. Example: @example', [
      '@example' => '&#60;div&#62;&#60;img src="http://www.example.com/sites/default/files/company_logo.png" width="300px" height="50px" &#62;&#60;/div&#62; &#60;hr /&#62;',
    ]),
    '#default_value' => $settings['pdf_header'],
  ];

  // Footer.
  $form['pdf']['head_foot']['pdf_footer'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Footer content'),
    '#description' => $this
      ->t('Use a valid HTML code to write a custom footer content. Example: @example', [
      'example' => '&#60;hr /&#62; &#60;div style="color:#f00; text-align:center;" &#62; &#60;strong&#62;Your Company&#60;/strong&#62;, web: &#60;a href="http://example.com"&#62;www.example.com&#60;/a&#62;, email : contact@example.com&#60;/div&#62;',
    ]),
    '#default_value' => $settings['pdf_footer'],
  ];

  // Password.
  $form['pdf']['permission'] = [
    '#type' => 'details',
    '#open' => FALSE,
    '#title' => $this
      ->t('PDF Password Protection'),
  ];
  $pwd = $settings['pdf_password'];
  if (isset($pwd) && $pwd != NULL) {
    $form['pdf']['permission']['msg'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('<p>***** Password is already set *****</p>'),
    ];
    $form['pdf']['permission']['remove_pwd'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Remove Password'),
    ];
  }
  else {
    $form['pdf']['permission']['pdf_password'] = [
      '#type' => 'password_confirm',
      '#description' => $this
        ->t('If password is not required, leave blank. Do not use space in starting and ending of password.'),
    ];
  }

  // Custom style Sheet.
  $form['pdf']['style'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom Style Sheet for PDF'),
    '#open' => FALSE,
  ];
  $form['pdf']['style']['pdf_css_file'] = [
    '#type' => 'textfield',
    '#description' => $this
      ->t('Enter path for CSS file. This path should be strictly related to Drupal root. For example: <em>styles/custom.css</em> will have its path validated as <em>DRUPAL_ROOT/styles/custom.css</em>'),
    '#default_value' => $settings['pdf_css_file'],
  ];
  $types = $this->entityTypeManager
    ->getStorage('node_type')
    ->loadMultiple();
  $options = [];
  foreach ($types as $type) {
    $options[$type
      ->id()] = $type
      ->label();
  }

  // Enabled content types.
  $form['pdf']['content_types'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Content types'),
    '#open' => FALSE,
  ];
  $form['pdf']['content_types']['enabled_content_types'] = [
    '#title' => $this
      ->t('Enable PDF generation for the following node types.'),
    '#type' => 'select',
    '#multiple' => TRUE,
    '#options' => $options,
    '#default_value' => $settings['enabled_content_types'],
  ];
  return parent::buildForm($form, $form_state);
}