You are here

class PrintfriendlyConfigForm in PrintFriendly & PDF 8

Same name and namespace in other branches
  1. 8.3 src/Form/PrintfriendlyConfigForm.php \Drupal\printfriendly\Form\PrintfriendlyConfigForm
  2. 8.2 src/Form/PrintfriendlyConfigForm.php \Drupal\printfriendly\Form\PrintfriendlyConfigForm

Hierarchy

Expanded class hierarchy of PrintfriendlyConfigForm

1 file declares its use of PrintfriendlyConfigForm
printfriendly.module in ./printfriendly.module
Adds PrintFriendly button to chosen node types and provides a block.
1 string reference to 'PrintfriendlyConfigForm'
printfriendly.routing.yml in ./printfriendly.routing.yml
printfriendly.routing.yml

File

src/Form/PrintfriendlyConfigForm.php, line 16
Contains \Drupal\printfriendly\Form\printfriendlyConfigForm.

Namespace

Drupal\printfriendly\Form
View source
class PrintfriendlyConfigForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'printfriendly_config_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('printfriendly.settings');
    $form = parent::buildForm($form, $form_state);
    $form['printfriendly_types'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Content Types'),
      '#description' => t('Configure where the printfriendly button should appear.'),
      '#options' => node_type_get_names(),
      '#default_value' => $config
        ->get('printfriendly_types', array()),
    );
    $form['printfriendly_display'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Button display'),
      '#description' => t('Select content displays that the button should appear.'),
      '#options' => array(
        'teaser' => t('Teaser'),
        'full' => t('Full content page'),
      ),
      '#default_value' => array_filter($config
        ->get('printfriendly_display')),
    );

    // Add more features here
    $form['printfriendly_features'] = array(
      '#type' => 'fieldset',
      '#title' => t('Features'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['printfriendly_features']['printfriendly_page_header'] = array(
      '#type' => 'select',
      '#title' => t('Page header'),
      '#options' => array(
        'default_logo' => 'My Website Icon',
        'custom_logo' => 'Upload an Image',
      ),
      '#default_value' => $config
        ->get('printfriendly_page_header', 'default_logo'),
    );
    $form['printfriendly_features']['printfriendly_page_custom_header'] = array(
      '#type' => 'textfield',
      '#title' => t('Enter URL'),
      '#description' => t('Put full path of the file like http://devt.drupalchamp.org/sites/default/files/drupal-logo.png'),
      '#states' => array(
        'invisible' => array(
          ':input[name="printfriendly_page_header"]' => array(
            array(
              'value' => t('default_logo'),
            ),
          ),
        ),
      ),
      '#default_value' => $config
        ->get('printfriendly_page_custom_header', ''),
    );
    $form['printfriendly_features']['printfriendly_tagline'] = array(
      '#type' => 'textfield',
      '#title' => t('Header tagline'),
      '#default_value' => $config
        ->get('printfriendly_tagline', ''),
      '#description' => t('Add a specific tagline to the header.'),
      '#states' => array(
        'invisible' => array(
          ':input[name="printfriendly_page_header"]' => array(
            array(
              'value' => t('default_logo'),
            ),
          ),
        ),
      ),
    );
    $form['printfriendly_features']['printfriendly_click_delete'] = array(
      '#type' => 'select',
      '#title' => t('Click-to-delete'),
      '#options' => array(
        '0' => 'Allow',
        '1' => 'Not Allow',
      ),
      '#default_value' => $config
        ->get('printfriendly_click_delete', '0'),
    );
    $form['printfriendly_features']['printfriendly_images'] = array(
      '#type' => 'select',
      '#title' => t('Images'),
      '#options' => array(
        '0' => 'Include',
        '1' => 'Exclude',
      ),
      '#default_value' => $config
        ->get('printfriendly_images', '0'),
    );
    $form['printfriendly_features']['printfriendly_image_style'] = array(
      '#type' => 'select',
      '#title' => t('Image style'),
      '#options' => array(
        'right' => 'Align Right',
        'left' => 'Align Left',
        'none' => 'Align None',
        'block' => 'Center/Block',
      ),
      '#default_value' => $config
        ->get('printfriendly_image_style', 'right'),
    );
    $form['printfriendly_features']['printfriendly_email'] = array(
      '#type' => 'select',
      '#title' => t('Email'),
      '#options' => array(
        '0' => 'Allow',
        '1' => 'Not Allow',
      ),
      '#default_value' => $config
        ->get('printfriendly_email', '0'),
    );
    $form['printfriendly_features']['printfriendly_pdf'] = array(
      '#type' => 'select',
      '#title' => t('PDF'),
      '#options' => array(
        '0' => 'Allow',
        '1' => 'Not Allow',
      ),
      '#default_value' => $config
        ->get('printfriendly_pdf', '0'),
    );
    $form['printfriendly_features']['printfriendly_print'] = array(
      '#type' => 'select',
      '#title' => t('Print'),
      '#options' => array(
        '0' => 'Allow',
        '1' => 'Not Allow',
      ),
      '#default_value' => $config
        ->get('printfriendly_print', '0'),
    );
    $form['printfriendly_features']['printfriendly_custom_css'] = array(
      '#type' => 'textfield',
      '#description' => t('Put full path of the file like http://devt.drupalchamp.org/sites/default/files/printfriendly.css'),
      '#title' => t('Custom css url'),
      '#default_value' => $config
        ->get('printfriendly_custom_css', ''),
    );
    $form['printfriendly_features']['printfriendly_website_protocol'] = array(
      '#type' => 'select',
      '#title' => t('Website protocol'),
      '#options' => array(
        'http' => 'HTTP',
        'https' => 'HTTPS',
      ),
      '#default_value' => $config
        ->get('printfriendly_website_protocol', 'http'),
    );
    $img_path = drupal_get_path('module', 'printfriendly') . '/images';
    $results = file_scan_directory($img_path, '/^.*\\.(gif|png|jpg|GIF|PNG|JPG)$/');
    $options = array();
    foreach ($results as $image) {
      $options[$image->filename] = '<img src="' . file_create_url($image->uri) . '" />';
    }
    ksort($options);
    $form['printfriendly_image'] = array(
      '#type' => 'radios',
      '#title' => t('Choose button'),
      '#options' => $options,
      '#default_value' => $config
        ->get('printfriendly_image', 'button-print-grnw20.png'),
    );
    $form['support-link'] = array(
      '#markup' => 'Need help or have suggestions? <a href="mailto:support@printfriendly.com">Support@PrintFriendly.com</a>',
      '#weight' => 1000,
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('printfriendly.settings');
    $config
      ->set('printfriendly_types', $form_state
      ->getValue('printfriendly_types'));
    $config
      ->set('printfriendly_display', $form_state
      ->getValue('printfriendly_display'));
    $config
      ->set('printfriendly_page_header', $form_state
      ->getValue('printfriendly_page_header'));
    $config
      ->set('printfriendly_page_custom_header', $form_state
      ->getValue('printfriendly_page_custom_header'));
    $config
      ->set('printfriendly_tagline', $form_state
      ->getValue('printfriendly_tagline'));
    if ($form_state
      ->getValue('printfriendly_page_header') == 'default_logo') {
      $config
        ->set('printfriendly_page_custom_header', '');
      $config
        ->set('printfriendly_tagline', '');
    }
    $config
      ->set('printfriendly_click_delete', $form_state
      ->getValue('printfriendly_click_delete'));
    $config
      ->set('printfriendly_images', $form_state
      ->getValue('printfriendly_images'));
    $config
      ->set('printfriendly_image_style', $form_state
      ->getValue('printfriendly_image_style'));
    $config
      ->set('printfriendly_email', $form_state
      ->getValue('printfriendly_email'));
    $config
      ->set('printfriendly_pdf', $form_state
      ->getValue('printfriendly_pdf'));
    $config
      ->set('printfriendly_print', $form_state
      ->getValue('printfriendly_print'));
    $config
      ->set('printfriendly_custom_css', $form_state
      ->getValue('printfriendly_custom_css'));
    $config
      ->set('printfriendly_website_protocol', $form_state
      ->getValue('printfriendly_website_protocol'));
    $config
      ->set('printfriendly_image', $form_state
      ->getValue('printfriendly_image'));
    $config
      ->save();
    return parent::submitForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function getEditableConfigNames() {
    return [
      'printfriendly.settings',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PrintfriendlyConfigForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
PrintfriendlyConfigForm::getEditableConfigNames public function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
PrintfriendlyConfigForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PrintfriendlyConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.