You are here

public function AdminSettingsForm::buildForm in PDF using mPDF 8.2

Same name and namespace in other branches
  1. 8 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 28

Class

AdminSettingsForm

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');

  // Vertical tab
  $form['tab'] = [
    '#type' => 'vertical_tabs',
  ];
  $form['tab_file_elements'] = [
    '#type' => 'details',
    '#title' => t('File'),
    '#group' => 'tab',
  ];
  $filename = $settings['pdf_filename'];
  $form['tab_file_elements']['pdf_filename'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('PDF Filename'),
    '#required' => TRUE,
    '#default_value' => isset($filename) && $filename != NULL ? $filename : '[site:name] - PDF',
  ];
  $form['tab_file_elements']['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['tab_document_properties_elements'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Document Properties'),
    '#description' => $this
      ->t('These optional properties can be seen when inspecting the document properties like in Adobe Reader.'),
    '#group' => 'tab',
  ];
  $form['tab_document_properties_elements']['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['tab_document_properties_elements']['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['tab_document_properties_elements']['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['tab_document_properties_elements']['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['tab_page_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Page Settings'),
    '#description' => $this
      ->t('All margin values should be specified as LENGTH in millimetres.'),
    '#group' => 'tab',
  ];
  $form['tab_page_settings']['margin_top'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Top Margin'),
    '#default_value' => $settings['margin_top'],
  ];
  $form['tab_page_settings']['margin_right'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Right Margin'),
    '#default_value' => $settings['margin_right'],
  ];
  $form['tab_page_settings']['margin_bottom'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Bottom Margin'),
    '#default_value' => $settings['margin_bottom'],
  ];
  $form['tab_page_settings']['margin_left'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Left Margin'),
    '#default_value' => $settings['margin_left'],
  ];
  $form['tab_page_settings']['margin_header'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Header Margin'),
    '#default_value' => $settings['margin_header'],
  ];
  $form['tab_page_settings']['margin_footer'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Footer Margin'),
    '#default_value' => $settings['margin_footer'],
  ];
  $form['tab_page_settings']['pdf_font_size'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Font Size'),
    '#default_value' => $settings['pdf_font_size'],
  ];
  $form['tab_page_settings']['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
      ->get_default_fonts(),
    '#default_value' => $settings['pdf_default_font'],
  ];
  $form['tab_page_settings']['pdf_page_size'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Page Size'),
    '#options' => $this
      ->getPageSizeOptions(),
    '#default_value' => $settings['pdf_page_size'],
  ];
  $form['tab_page_settings']['dpi'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Document DPI'),
    '#default_value' => $settings['dpi'],
  ];
  $form['tab_page_settings']['img_dpi'] = [
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => $this
      ->t('Image DPI'),
    '#default_value' => $settings['img_dpi'],
  ];
  $form['tab_page_settings']['orientation'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Orientation'),
    '#default_value' => $settings['orientation'],
    '#options' => [
      'P' => $this
        ->t('Portrait'),
      'L' => $this
        ->t('Landscape'),
    ],
  ];

  //TODO: image size options in admin form for watermark

  // Watermark.
  $form['tab_watermark'] = [
    '#type' => 'details',
    '#description' => $this
      ->t('Display watermark on every page.'),
    '#title' => $this
      ->t('Watermark'),
    '#group' => 'tab',
  ];
  $form['tab_watermark']['watermark_opacity'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Watermark Transparency'),
    '#default_value' => $settings['watermark_opacity'],
  ];
  $form['tab_watermark']['watermark_option'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Watermark'),
    '#options' => [
      $this
        ->t('Text'),
      $this
        ->t('Image'),
    ],
    '#default_value' => $settings['watermark_option'],
  ];
  $form['tab_watermark']['pdf_watermark_text'] = [
    '#type' => 'textfield',
    '#default_value' => $settings['pdf_watermark_text'],
    '#placeholder' => $this
      ->t('Watermark text'),
    '#states' => [
      'visible' => [
        ':input[name="watermark_option"]' => [
          'value' => 0,
        ],
      ],
    ],
  ];
  $form['tab_watermark']['watermark_image'] = [
    '#type' => 'managed_file',
    '#default_value' => !empty($settings['watermark_image']) ? $settings['watermark_image'] : [],
    '#upload_location' => 'public://pdf_using_mpdf',
    '#upload_validators' => [
      'file_validate_extensions' => [
        'gif png jpg jpeg',
      ],
      'file_validate_size' => [
        1024 * 1024,
      ],
    ],
    '#states' => [
      'visible' => [
        ':input[name="watermark_option"]' => [
          'value' => 1,
        ],
      ],
    ],
  ];

  // Header.
  $form['tab_head'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Header'),
    '#description' => $this
      ->t('use {PAGENO} for page numbering or {DATE j-m-Y} for current date.'),
    '#group' => 'tab',
  ];
  $form['tab_head']['pdf_header'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Header content'),
    '#description' => $this
      ->t('Use a valid HTML code to write a custom header content. 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['tab_foot'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Footer'),
    '#description' => $this
      ->t('use {PAGENO} for page numbering or {DATE j-m-Y} for current date.'),
    '#group' => 'tab',
  ];
  $form['tab_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:') . ' &#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['tab_password'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Password Protection'),
    '#group' => 'tab',
  ];
  $pwd = !empty($settings['pdf_password']) ? $settings['pdf_password'] : NULL;
  if (isset($pwd) && $pwd != NULL) {
    $form['tab_password']['msg'] = [
      '#type' => 'markup',
      '#markup' => '<p>' . $this
        ->t('***** Password is already set *****') . '</p>',
    ];
    $form['tab_password']['remove_pwd'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Remove Password'),
    ];
  }
  else {
    $form['tab_password']['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['tab_style'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom Style Sheet'),
    '#open' => FALSE,
    '#group' => 'tab',
  ];
  $form['tab_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'],
  ];

  // Support for Token contrib module to provide a Browse UI
  if (\Drupal::service('module_handler')
    ->moduleExists('devel')) {
    $form['available_tokens'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'node',
      ],
    ];
  }
  else {
    $url = Url::fromUri('https://www.drupal.org/project/token');
    $form['available_tokens'] = [
      '#prefix' => '<em>',
      '#markup' => t('Tip: Install @token_url module to browse more tokens', [
        '@token_url' => Link::fromTextAndUrl('Token', $url)
          ->toString(),
      ]),
      '#suffix' => '</em>',
    ];
  }
  return parent::buildForm($form, $form_state);
}