jfu-1.0.x-dev/src/Plugin/Field/FieldWidget/JSONComponentWidget.php
src/Plugin/Field/FieldWidget/JSONComponentWidget.php
<?php
namespace Drupal\jfu\Plugin\Field\FieldWidget;
use Drupal\Core\Config\ConfigFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\Core\Extension\ExtensionPathResolver;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\json_field\Plugin\Field\FieldWidget\JsonTextareaWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\jfu\Services\JfuConfigServices;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
/**
* Plugin implementation of the 'json_component_widget' widget.
*
* @FieldWidget(
* id = "json_component_widget",
* module = "jfu",
* label = @Translation("Component"),
* description = @Translation("Define all components."),
* field_types = {
* "json",
* "json_native",
* "json_native_binary"
* },
* )
*/
class JSONComponentWidget extends JsonTextareaWidget {
/**
* {@inheritdoc}
*/
public function __construct(
$plugin_id,
$plugin_definition,
FieldDefinitionInterface $field_definition,
array $settings,
array $third_party_settings,
ElementInfoManagerInterface $element_info,
ExtensionPathResolver $extension_path_resolver,
ConfigFactory $configFactory,
JfuConfigServices $jfu_config) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->elementInfo = $element_info;
$this->extensionPathResolver = $extension_path_resolver;
$this->configFactory = $configFactory;
$this->jfuConfig = $jfu_config;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['third_party_settings'],
$container->get('element_info'),
$container->get('extension.path.resolver'),
$container->get('config.factory'),
$container->get('jfu_config.service')
);
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
if (!isset($form['#is_jfu'])) {
$form['#is_jfu'] = 'field--widget-json-component-widget';
}
$config = $this->jfuGetConfig();
$components = !is_null($config->get('components')) ? $config->get('components') : [];
$components_config = !is_null($config->get('components_config')) ? $config->get('components_config') : [];
$jfu_components = [];
foreach ($components as $name) {
if ($name !== 0) {
$module_name = 'jfu';
if(isset($components_config[$name]['module_name']) && !empty($components_config[$name]['module_name'])) {
$module_name = $components_config[$name]['module_name'];
}
$cmd = DRUPAL_ROOT . '/' . $this->extensionPathResolver->getPath('module', $module_name) . '/component_models/';
$ufc = $cmd . $name . '/' . $name . '.json';
$json_string = file_get_contents($ufc);
$jfu_components[$name] = Json::decode($json_string);
//TODO: Key module name is added in the json to select the corresponding template avaluate other options.
$jfu_components[$name]['module_name'] = $module_name;
}
}
$form['#attached']['drupalSettings']['jfu_components'][$this->fieldDefinition->getName()] = $jfu_components;
$element['value'] = $element + [
'#type' => 'textarea',
'#default_value' => $items[$delta]->value,
'#attributes' => [
'target-weight' => $delta,
],
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$url = Url::fromRoute('jfu.config')->setRouteParameters([
'entity_type' => $this->fieldDefinition->get('entity_type'),
'bundle' => $this->fieldDefinition->get('bundle'),
'field_name' => $this->fieldDefinition->getName(),
])->toString();
$element['jfu_link'] = [
'#type' => 'markup',
'#markup' => '<br><a class="use-ajax" data-dialog-options="{"width":800,"classes":{"ui-dialog":"jfu-modal-config"}}" data-dialog-type="modal" v-bind:href="' . $url . '">' . $this->t('Edit configurations') .'</a>',
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$config = $this->jfuGetConfig();
if (!empty($config->get('components'))) {
$items = [];
foreach ($config->get('components') as $component) {
if ($component !== 0) {
$items[] = ucfirst(str_replace('_', ' ', $component));
}
}
$summary[] = [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#title' => $this->t('Allowed components'),
'#items' => $items,
];
}
else {
$summary[] = $this->t("You don't have any selected components for this field.");
$summary[] = $this->t('Choose the components you want to use.');
}
return $summary;
}
/**
* {@inheritdoc}
*/
private function jfuGetConfig() {
$entity_type = $this->fieldDefinition->get('entity_type');
$bundle = $this->fieldDefinition->get('bundle');
$field_name = $this->fieldDefinition->getName();
$config_name = $this->jfuConfig->jfuConfigName($entity_type, $bundle, $field_name);
$config = $this->configFactory->getEditable($config_name);
return $config;
}
}
