Form builder; Configure advagg settings.
See also
system_settings_form()
Related topics
1 string reference to 'advagg_critical_css_admin_settings_form'
- advagg_critical_css_menu in advagg_critical_css/
advagg_critical_css.module - Implements hook_menu().
File
-
advagg_critical_css/
advagg_critical_css.admin.inc, line 15
Code
function advagg_critical_css_admin_settings_form() {
drupal_set_title(t('AdvAgg: Critical CSS'));
advagg_display_message_if_requirements_not_met();
$form = array();
$default_theme = variable_get('theme_default', 'bartik');
$global_theme = $GLOBALS['theme'];
$themes = array_keys(list_themes());
$form['#attached']['css'][] = array(
'data' => ".form-item-lookup{padding-bottom:0;margin-bottom:0;}",
'type' => 'inline',
);
$form['add_item']['theme'] = array(
'#type' => 'select',
'#title' => t('Theme'),
'#options' => array_combine($themes, $themes),
'#default_value' => $default_theme,
'#description' => t('Theme Default: %default, Current Theme: %current', array(
'%default' => $default_theme,
'%current' => $global_theme,
)),
);
$form['add_item']['user'] = array(
'#type' => 'select',
'#title' => t('User type'),
'#default_value' => 0,
'#options' => array(
'anonymous' => t('anonymous'),
'authenticated' => t('authenticated'),
'all' => t('all'),
),
);
$type_options = array(
0 => t('Disabled'),
2 => t('URL'),
8 => t('Node Type'),
);
$form['add_item']['type'] = array(
'#type' => 'select',
'#title' => t('Type of lookup'),
'#default_value' => 2,
'#options' => $type_options,
);
$form['add_item']['lookup'] = array(
'#type' => 'textfield',
'#title' => t('Value to lookup'),
'#maxlength' => 255,
'#states' => array(
'disabled' => array(
':input[name="type"]' => array(
'value' => 0,
),
),
),
);
$form['add_item']['lookup_container_disabled'] = array(
'#type' => 'container',
'#states' => array(
'visible' => array(
':input[name="type"]' => array(
'value' => 0,
),
),
),
);
$form['add_item']['lookup_container_disabled']['disabled'] = array(
'#markup' => '<br>',
);
$form['add_item']['lookup_container_current_path'] = array(
'#type' => 'container',
'#states' => array(
'visible' => array(
':input[name="type"]' => array(
'value' => 2,
),
),
),
);
$form['add_item']['lookup_container_current_path']['current_path'] = array(
'#markup' => t('%front is the front page; can use internal URLs like %internal or an alias like %here', array(
'%front' => '<front>',
'%internal' => 'node/2',
'%here' => current_path(),
)),
);
$form['add_item']['lookup_container_node_type'] = array(
'#type' => 'container',
'#states' => array(
'visible' => array(
':input[name="type"]' => array(
'value' => 8,
),
),
),
);
$form['add_item']['lookup_container_node_type']['node_type'] = array(
'#markup' => t('Node type is the machine name of the node; list of node types: @node_types', array(
'@current_path' => 'https://api.drupal.org/api/drupal/includes%21path.inc/function/current_path/7.x',
'@request_path' => 'https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/request_path/7.x',
'@request_uri' => 'https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/request_uri/7.x',
'@node_types' => implode(', ', array_keys(node_type_get_names())),
)),
);
$form['add_item']['css'] = array(
'#type' => 'textarea',
'#title' => t('Critical CSS'),
'#description' => t('Can be generated via <a href="@url">https://www.sitelocity.com/critical-path-css-generator</a>. If this field is empty this entry will be deleted.', array(
'@url' => 'https://www.sitelocity.com/critical-path-css-generator',
)),
'#default_value' => '',
);
$form['add_item']['dns'] = array(
'#type' => 'textarea',
'#title' => t('Hostnames to lookup'),
'#description' => t('Hosts that will be connected to.'),
'#default_value' => '',
);
$form['add_item']['pre'] = array(
'#type' => 'textarea',
'#title' => t('Urls to Preload'),
'#description' => t('Assets for the browser that should be downloaded at a high priority.'),
'#default_value' => '',
);
// Lookup saved data.
$query = db_select('advagg_critical_css', 'acc')->fields('acc')
->comment('Query called from ' . __FUNCTION__ . '()');
$results = $query->execute();
// Put results into array.
$counter = 0;
foreach ($results as $row) {
$counter++;
$row = (array) $row;
foreach ($form['add_item'] as $key => $values) {
// Fix the states array for type.
if (!empty($values['#states'])) {
foreach ($values['#states'] as $states_key => $states_values) {
$states_value = reset($values['#states'][$states_key]);
$values['#states'][$states_key] = array(
":input[name=\"{$counter}_type\"]" => $states_value,
);
}
}
$form['existing_items'][$counter]["{$counter}_{$key}"] = $values;
if (isset($row[$key])) {
$form['existing_items'][$counter]["{$counter}_{$key}"]['#default_value'] = $row[$key];
}
}
// Add in css to move the text hint up.
$form['#attached']['css'][] = array(
'data' => ".form-item-{$counter}-lookup{padding-bottom:0;margin-bottom:0;}",
'type' => 'inline',
);
// Add fieldset.
$filename = advagg_url_to_filename($row['lookup'], FALSE);
$base = drupal_get_path('theme', $row['theme']) . "/critical-css/{$row['user']}/";
if ($row['type'] == 2) {
$base .= "urls/{$filename}";
}
elseif ($row['type'] == 8) {
$base .= "type/{$filename}";
}
else {
$base = '';
}
$form['existing_items'][$counter] += array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('@type @theme @user @lookup', array(
'@theme' => $row['theme'],
'@type' => $type_options[$row['type']],
'@user' => $row['user'],
'@lookup' => $row['lookup'],
)),
);
if (!empty($base)) {
$form['existing_items'][$counter]['#description'] = t('If you wish to store this configuration in a file<br>Critical CSS: <code>@css</code>', array(
'@css' => "{$base}.css",
));
if (!empty($row['dns'])) {
$form['existing_items'][$counter]['#description'] .= t('<br>Hostnames: <code>@dns</code>', array(
'@dns' => "{$base}.dns",
));
}
if (!empty($row['pre'])) {
$form['existing_items'][$counter]['#description'] .= t('<br>Preload: <code>@pre</code>', array(
'@pre' => "{$base}.pre",
));
}
}
}
// Add top level fieldsets.
$form['add_item'] += array(
'#type' => 'fieldset',
'#title' => t('Add Critical CSS'),
'#collapsible' => TRUE,
'#collapsed' => $results->rowCount(),
);
if (!empty($form['existing_items'])) {
$form['existing_items'] += array(
'#type' => 'fieldset',
'#title' => t('Edit Critical CSS'),
);
}
$form['advagg_critical_css_selector_blacklist'] = array(
'#type' => 'textarea',
'#title' => t('Selector Blacklist'),
'#description' => t('Selectors to exclude. Enter one per line. Useful for things like google ads.'),
'#default_value' => variable_get('advagg_critical_css_selector_blacklist', ''),
);
// Clear the cache bins on submit.
$form['#submit'][] = 'advagg_critical_css_admin_settings_form_submit';
// Most code below taken from system_settings_form().
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
$form['actions']['disable'] = array(
'#type' => 'submit',
'#value' => t('Disable All From Database'),
'#submit' => array(
'advagg_critical_css_admin_settings_form_submit_disable',
),
);
if (!empty($_POST) && form_get_errors()) {
drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
}
// By default, render the form using theme_system_settings_form().
if (!isset($form['#theme'])) {
$form['#theme'] = 'system_settings_form';
}
return $form;
}