Last active
September 7, 2016 08:23
-
-
Save vlledo/8bf741ed56022cb0eda2f178a428629a to your computer and use it in GitHub Desktop.
Example of include new fields in theme settings for Drupal 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MY_MODULE_form_system_theme_settings_alter(&$form, FormStateInterface $form_state, $form_id = NULL) { | |
// Work-around for a core bug affecting admin themes. See issue #943212. | |
if (isset($form_id)) { | |
return; | |
} | |
$user = Drupal::currentUser()->id(); | |
$access = $user == 1 ? TRUE : FALSE; | |
$form['page_sidebar'] = array( | |
'#type' => 'details', | |
'#title' => t('Configuración del tipo de página'), | |
'#open' => TRUE, | |
); | |
$form['page_sidebar']['sidebar'] = array( | |
'#type' => 'radios', | |
'#title' => t('Portada con sidebar'), | |
'#default_value' => theme_get_setting('sidebar'), | |
'#options' => array(0 => t('No'), 1 => t('Sí')), | |
); | |
$form['page_sidebar']['menu_horizontal'] = array( | |
'#type' => 'radios', | |
'#title' => t('Menú horizontal'), | |
'#default_value' => theme_get_setting('menu_horizontal'), | |
'#options' => array(0 => t('No'), 1 => t('Sí')), | |
); | |
$form['page_header'] = array( | |
'#type' => 'details', | |
'#title' => t('Configuración de la cabecera del sitio'), | |
'#open' => TRUE, | |
); | |
if (!is_null(theme_get_setting('contact'))) { | |
$contact = \Drupal\node\Entity\Node::load(theme_get_setting('contact')); | |
$url = Url::fromRoute('entity.node.edit_form', array('node' => $contact->nid->value)); | |
$link = Link::fromTextAndUrl('Editar mi información de contacto', $url); | |
$link = $link->toString(); | |
} else { | |
$contact = NULL; | |
$link = ''; | |
} | |
$form['page_header']['contact'] = array( | |
'#type' => 'entity_autocomplete', | |
'#target_type' => 'node', | |
'#title' => t('Información de contacto'), | |
'#bundles' => array('ujaen_contacto_entidad'), | |
'#default_value' => $contact, | |
'#description' => $link, | |
); | |
$fid = theme_get_setting('image'); | |
if (!empty($fid)) { | |
$fid = intval($fid['0']); | |
} else { | |
$fid = 0; | |
} | |
$form['page_header']['image'] = array( | |
'#type' => 'managed_file', | |
'#title' => t('Imagen para la cabecera'), | |
'#default_value' => array($fid), | |
'#description' => t('Sube una imagen para la cabecera del sitio'), | |
'#upload_location' => 'public://images/', | |
'#upload_validators' => array( | |
'file_validate_extensions' => array('gif png jpg jpeg'), | |
'file_validate_size' => array(25600000), | |
), | |
); | |
$form['page_header']['image_alt'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Texto alternativo'), | |
'#placeholder' => t('Texto alternativo'), | |
'#description' => t('Indica un texto descriptivo de la imagen de cabecera del sitio.'), | |
); | |
$form['page_header']['background_color'] = array( | |
'#type' => 'select', | |
'#title' => t('Color de fondo'), | |
'#default_value' => theme_get_setting('background_color'), | |
'#description' => t('Elige el color de fondo para la cabecera del sitio'), | |
'#options' => [ | |
'#205383' => t('Azul'), | |
'#A68508' => t('Dorado'), | |
'#822633' => t('Rojo'), | |
'#5F115C' => t('Morado'), | |
'#443A32' => t('Gris oscuro'), | |
'#3C726B' => t('Azul turquesa'), | |
], | |
); | |
$fid_logo = theme_get_setting('logo_subsitio'); | |
if (!empty($fid_logo)) { | |
$fid_logo = intval($fid_logo['0']); | |
} else { | |
$fid_logo = 0; | |
} | |
$form['page_header']['logo_subsitio'] = array( | |
'#type' => 'managed_file', | |
'#title' => t('Logotipo del sitio.'), | |
'#default_value' => array($fid_logo), | |
'#description' => t('Esta imagen solo podrá establecerla/cambiarla un usuario administrador. Sólo se admite el formato png. La imagen deberá contar con un mínimo de 70 píxeles de anchura'), | |
'#upload_location' => 'public://images/', | |
'#upload_validators' => array( | |
'file_validate_extensions' => array('png'), | |
'file_validate_size' => array(25600000), | |
), | |
'#access' => $access, | |
); | |
$form['theme_settings']['#access'] = $access; | |
$form['logo']['#access'] = $access; | |
$form['favicon']['#access'] = $access; | |
$form['page_header']['html_markup'] = array('#markup' => t('<p>Recuerde que también dispone de la posibilidad de establecer un logotipo para la cabecera. Contacte con el Gabinete de Comunicación y Proyección Institucional para conocer los casos uso establecidos del campo logo.</p>')); | |
// Importante establecer la imagen como permanente -> http://www.linnovate.net/blog/drupal-and-disappearing-images-mystery | |
$form['#submit'][] = '_my_module_form_system_theme_settings_alter_submit'; | |
} | |
function _my_module_form_system_theme_settings_alter_submit($form, FormStateInterface $form_state) { | |
\Drupal\Core\Cache\Cache::invalidateTags(array('subsite_header')); | |
if ($image = $form_state->getValue('image')) { | |
$fid = reset($image); | |
$file = File::load($fid); | |
$file->setPermanent(); | |
$file->save(); | |
$file_usage = Drupal::service('file.usage'); | |
$file_usage->add($file, 'ujaen_subsite_theme', 'theme', 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment