templating-8.x-2.0/src/Form/ConfigTemplateSetting.php
src/Form/ConfigTemplateSetting.php
<?php
namespace Drupal\templating\Form;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Edit config variable form.
*/
class ConfigTemplateSetting extends FormBase
{
protected $step = -1;
/**
* {@inheritdoc}
*/
public function getFormId()
{
return 'config_template_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $config_name = '')
{
$config_settings = \Drupal::config("template_inline.settings") ;
$services = \Drupal::service('templating.manager');
echo $config_settings->get('enabled_lib');
$form['enabled_lib'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Library'),
'#default_value' => ($config_settings->get('enabled_lib'))? $config_settings->get('enabled_lib'): false
);
$form['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Disable template inline'),
'#default_value' => ($config_settings->get('disable'))? $config_settings->get('disable'): false
);
$form['actions'] = ['#type' => 'actions'];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
$form['actions']['cancel'] = array(
'#type' => 'link',
'#title' => $this->t('Back to Template list'),
'#url' => $this->buildCancelLinkUrl(),
);
return $form;
}
/**
* Builds the cancel link url for the form.
*
* @return Url
* Cancel url
*/
private function buildCancelLinkUrl()
{
$query = $this->getRequest()->query;
if ($query->has('destination')) {
$options = UrlHelper::parse($query->get('destination'));
$url = Url::fromUri('internal:/' . $options['path'], $options);
} else {
// $url = Url::fromRoute('templating.manager');
}
return $url;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state)
{
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
$values = $form_state->getValues();
// saving section
// $libraries = array();
// $themes = system_list('theme');
// foreach ($themes as $key => $value) {
// $theme_config = $key.".settings_library";
// $config = \Drupal::config( $theme_config);
// $spt_table = $config->get('spt_table');
// if(!empty($spt_table)){
// foreach ($spt_table as $key_lib => $item_lib) {
// if($item_lib['status'] == 1){
// if(!isset($libraries[$item_lib['name']])){
// $str[$item_lib['name']] = "{{ attach_library('mz_lib/".$item_lib['name']."') }}<br/>";
// }
// }
// }
// }
// }
$content_lib = "" ;
if(isset($values['enabled_lib']) && $values['enabled_lib'] == 1){
$entityTypeManager = \Drupal::service('entity_type.manager');
$types = [];
$contentTypes = $entityTypeManager->getStorage('node_type')->loadMultiple();
$is_exist = false ;
foreach ($contentTypes as $contentType) {
if($contentType->id() == 'library'){
$is_exist = true ;
}
}
if( $is_exist ){
//load your field
$field_theme = \Drupal\field\Entity\FieldStorageConfig::loadByName('node', 'field_theme');
$field_theme->setSetting('allowed_values_function','_templating_theme_allowed_values_function' );
$field_theme->save();
}
}
$this->configFactory()->getEditable('template_inline.settings')
->set('disable', $values['disable'])
->set('content_lib', $content_lib )
->set('enabled_lib', $values['enabled_lib'] )
->save();
}
}
