You are here

function flippy_form_node_type_edit_form_alter in Flippy 8

Implements hook_form_FORM_ID_alter().

File

./flippy.module, line 45
Allows administrators to add previous/next pagers to any node type.

Code

function flippy_form_node_type_edit_form_alter(&$form, FormStateInterface $form_state) {
  if (isset($form['type'])) {

    // Flippy settings.
    $flippy_settings = \Drupal::config('flippy.settings');
    $flippy_node_type = $flippy_settings
      ->get('flippy_' . $form['type']['#default_value']);
    $flippy_head = $flippy_settings
      ->get('flippy_head_' . $form['type']['#default_value']);
    $flippy_show_empty = $flippy_settings
      ->get('flippy_show_empty_' . $form['type']['#default_value']);
    $flippy_prev_label = $flippy_settings
      ->get('flippy_prev_label_' . $form['type']['#default_value']);
    $flippy_next_label = $flippy_settings
      ->get('flippy_next_label_' . $form['type']['#default_value']);
    $flippy_first_last = $flippy_settings
      ->get('flippy_first_last_' . $form['type']['#default_value']);
    $flippy_first_label = $flippy_settings
      ->get('flippy_first_label_' . $form['type']['#default_value']);
    $flippy_last_label = $flippy_settings
      ->get('flippy_last_label_' . $form['type']['#default_value']);
    $flippy_loop = $flippy_settings
      ->get('flippy_loop_' . $form['type']['#default_value']);
    $flippy_random = $flippy_settings
      ->get('flippy_random_' . $form['type']['#default_value']);
    $flippy_random_label = $flippy_settings
      ->get('flippy_random_label_' . $form['type']['#default_value']);
    $flippy_truncate = $flippy_settings
      ->get('flippy_truncate_' . $form['type']['#default_value']);
    $flippy_ellipse = $flippy_settings
      ->get('flippy_ellipse_' . $form['type']['#default_value']);
    $flippy_swipe = $flippy_settings
      ->get('flippy_press_swipe_' . $form['type']['#default_value']);
    $flippy_custom_sorting = $flippy_settings
      ->get('flippy_custom_sorting_' . $form['type']['#default_value']);
    $flippy_sort = $flippy_settings
      ->get('flippy_sort_' . $form['type']['#default_value']);
    $flippy_order = $flippy_settings
      ->get('flippy_order_' . $form['type']['#default_value']);
    $form['flippy'] = [
      '#type' => 'details',
      '#title' => t('Flippy settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#group' => 'additional_settings',
    ];
    $form['flippy']['flippy'] = [
      '#type' => 'checkbox',
      '#title' => t('Build a pager for this content type'),
      '#default_value' => isset($form['flippy']['flippy']) ? $form['flippy']['flippy'] : $flippy_node_type,
    ];
    $form['flippy']['flippy_head'] = [
      '#type' => 'checkbox',
      '#title' => t('Add semantic previous and next links to the document HEAD'),
      '#default_value' => isset($form['flippy']['flippy_head']) ? $form['flippy']['flippy_head'] : $flippy_head,
      '#states' => [
        'visible' => [
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['flippy']['flippy_show_empty'] = [
      '#type' => 'checkbox',
      '#title' => t('Show empty links'),
      '#default_value' => isset($form['flippy']['flippy_show_empty']) ? $form['flippy']['flippy_show_empty'] : $flippy_show_empty,
      '#states' => [
        'visible' => [
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#description' => t('If checked, empty links will be rendered even if there isn\'t a node in the sequence. For example, if there is no "next" node, the "next" label will be shown but without a link. If tokens are being used, it is recommended that this be unchecked.'),
    ];
    $form['flippy']['flippy_prev_label'] = [
      '#type' => 'textfield',
      '#title' => t('Label for "Previous" link'),
      '#size' => 32,
      '#default_value' => isset($form['flippy']['flippy_prev_label']) ? $form['flippy']['flippy_prev_label'] : $flippy_prev_label,
      '#states' => [
        'visible' => [
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['flippy']['flippy_next_label'] = [
      '#type' => 'textfield',
      '#title' => t('Label for "Next" link'),
      '#size' => 32,
      '#default_value' => isset($form['flippy']['flippy_next_label']) ? $form['flippy']['flippy_next_label'] : $flippy_next_label,
      '#states' => [
        'visible' => [
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['flippy']['flippy_firstlast'] = [
      '#type' => 'checkbox',
      '#title' => t('Show first/last links'),
      '#default_value' => isset($form['flippy']['flippy_firstlast']) ? $form['flippy']['flippy_firstlast'] : $flippy_first_last,
      '#states' => [
        'visible' => [
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['flippy']['flippy_first_label'] = [
      '#type' => 'textfield',
      '#title' => t('Label for "First" link'),
      '#size' => 32,
      '#default_value' => isset($form['flippy']['flippy_first_label']) ? $form['flippy']['flippy_first_label'] : $flippy_first_label,
      '#states' => [
        'visible' => [
          ':input[name=flippy_firstlast]' => [
            'checked' => TRUE,
          ],
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['flippy']['flippy_last_label'] = [
      '#type' => 'textfield',
      '#title' => t('Label for "Last" link'),
      '#size' => 32,
      '#default_value' => isset($form['flippy']['flippy_last_label']) ? $form['flippy']['flippy_last_label'] : $flippy_last_label,
      '#states' => [
        'visible' => [
          ':input[name=flippy_firstlast]' => [
            'checked' => TRUE,
          ],
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['flippy']['flippy_loop'] = [
      '#type' => 'checkbox',
      '#title' => t('Loop through nodes'),
      '#default_value' => isset($form['flippy']['flippy_loop']) ? $form['flippy']['flippy_loop'] : $flippy_loop,
      '#states' => [
        'visible' => [
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['flippy']['flippy_random'] = [
      '#type' => 'checkbox',
      '#title' => t('Show random link'),
      '#default_value' => isset($form['flippy']['flippy_random']) ? $form['flippy']['flippy_random'] : $flippy_random,
      '#states' => [
        'visible' => [
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['flippy']['flippy_random_label'] = [
      '#type' => 'textfield',
      '#title' => t('Label for "Random" link'),
      '#size' => 32,
      '#default_value' => isset($form['flippy']['flippy_random_label']) ? $form['flippy']['flippy_random_label'] : $flippy_random_label,
      '#states' => [
        'visible' => [
          ':input[name=flippy_random]' => [
            'checked' => TRUE,
          ],
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    if (\Drupal::moduleHandler()
      ->moduleExists('token')) {
      $form['flippy']['flippy_token'] = [
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#title' => t('Browse available tokens'),
        '#states' => [
          'visible' => [
            ':input[name=flippy]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $form['flippy']['flippy_token']['flippy_token_browser'] = [
        '#theme' => 'token_tree_link',
        '#token_types' => [
          'node',
        ],
      ];
    }
    $form['flippy']['flippy_truncate'] = [
      '#type' => 'textfield',
      '#title' => t('Truncate label length'),
      '#size' => 32,
      '#default_value' => isset($form['flippy']['flippy_truncate']) ? $form['flippy']['flippy_truncate'] : $flippy_truncate,
      '#description' => t('Optionally provide a maximum amount length that link labels can be. Labels will be shortened to this length if they exceed the amount of characters.'),
      '#states' => [
        'visible' => [
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['flippy']['flippy_ellipse'] = [
      '#type' => 'textfield',
      '#title' => t('Truncate ellipse'),
      '#size' => 32,
      '#default_value' => isset($form['flippy']['flippy_ellipse']) ? $form['flippy']['flippy_ellipse'] : $flippy_ellipse,
      '#description' => t('If a maximum label length is set above, an ellipse can be provided here which will be appended to the label after it is shortened.'),
      '#states' => [
        'visible' => [
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    if (\Drupal::moduleHandler()
      ->moduleExists('hammerjs')) {
      $form['flippy']['flippy_press_swipe'] = [
        '#type' => 'checkbox',
        '#title' => t('Support key press and swipe in mobile'),
        '#default_value' => isset($form['flippy']['flippy_press_swipe']) ? $form['flippy']['flippy_press_swipe'] : $flippy_swipe,
      ];
    }
    $form['flippy']['flippy_custom_sorting'] = [
      '#type' => 'checkbox',
      '#title' => t('Sort the pager by something other than ascending post date'),
      '#default_value' => isset($form['flippy']['flippy_custom_sorting']) ? $form['flippy']['flippy_custom_sorting'] : $flippy_custom_sorting,
      '#states' => [
        'visible' => [
          ':input[name=flippy]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    // Allow certain base table properties for sorting
    // $sort_options = _flippy_sorting_properties();
    $sort_options = [];

    // Also allow some entity fields.
    $content_type_fields = \Drupal::service('entity_field.manager')
      ->getFieldDefinitions('node', $form['type']['#default_value']);
    foreach ($content_type_fields as $sort_field) {
      if (get_class($sort_field) == 'Drupal\\field\\Entity\\FieldConfig') {

        // It is an additional field.
        $itemDefinition = $sort_field
          ->getFieldStorageDefinition();
        $schema_info = $itemDefinition
          ->getSchema();
      }
      else {
        if (get_class($sort_field) == 'Drupal\\Core\\Field\\BaseFieldDefinition') {

          // It is a base field.
          $schema_info = $sort_field
            ->getSchema();
        }
      }
      if (isset($schema_info['columns']['value']) && ($schema_info['columns']['value']['type'] == 'varchar' || $schema_info['columns']['value']['type'] == 'int' || $sort_field
        ->getType() == 'datetime')) {
        $sort_options[$sort_field
          ->getName()] = $sort_field
          ->getLabel();
      }
    }
    $form['flippy']['flippy_sort'] = [
      '#type' => 'select',
      '#title' => t('Pager sort'),
      '#options' => $sort_options,
      '#default_value' => isset($form['flippy']['flippy_sort']) ? $form['flippy']['flippy_sort'] : $flippy_sort,
      '#description' => t('Select a field for sorting the pager'),
      '#states' => [
        'visible' => [
          ':input[name=flippy_custom_sorting]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['flippy']['flippy_order'] = [
      '#type' => 'select',
      '#title' => t('Pager order'),
      '#options' => [
        'ASC' => t('Ascending'),
        'DESC' => t('Descending'),
      ],
      '#default_value' => isset($form['flippy']['flippy_order']) ? $form['flippy']['flippy_order'] : $flippy_order,
      '#description' => t('Select a direction to order the pager'),
      '#states' => [
        'visible' => [
          ':input[name=flippy_custom_sorting]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    // Add a custom submit handler to save the array of types back to the config
    // file.
    $form['actions']['submit']['#submit'][] = '_flippy_node_type_form_submit';
  }
}