/**
* Plugin implementation of the 'topic_widget' widget.
*
* @FieldWidget(
* id = "resource_widget",
* label = @Translation("Resource default"),
* field_types = {
* "resource"
* }
* )
*/
class ResourceDefaultWidget extends WidgetBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$config = \Drupal::config('resources.config');
$json_topics = file_get_contents($config->get('global_config.url_api') . '/topics');
$topics = Json::decode($json_topics);
$options_topics[0] = '--- Choose topic ---';
foreach ($topics as $key => $topic) {
$options_topics[$topic['name']] = ucfirst($topic['name']);
}
$element['topic'] = [
'#type' => 'select',
'#title' => t('Topic'),
'#description' => t('List of topics from osteoporosis-foundation'),
'#default_value' => isset($items[$delta]->topic) ? $items[$delta]->topic : NULL,
'#options' => $options_topics,
];
$json_materials = file_get_contents($config->get('global_config.url_api') . '/materials');
$materials = Json::decode($json_materials);
$options_mat[0] = '--- Choose material ---';
foreach ($materials as $key => $material) {
$options_mat[$material['name']] = ucfirst($material['name']);
}
$element['material'] = [
'#type' => 'select',
'#title' => t('Material'),
'#description' => t('List of materials from osteoporosis-foundation'),
'#default_value' => isset($items[$delta]->material) ? $items[$delta]->material : NULL,
'#options' => $options_mat,
];
$json_add_infos = file_get_contents($config->get('global_config.url_api') . '/additional-information');
$add_infos = Json::decode($json_add_infos);
$options_add_infos[0] = '--- Choose additional information ---';
foreach ($add_infos as $key => $info) {
$options_add_infos[$info['name']] = ucfirst($info['name']);
}
$element['add_infos'] = [
'#type' => 'select',
'#title' => t('Additional Information'),
'#multiple' => TRUE,
'#description' => t('List of additional information from osteoporosis-foundation'),
'#default_value' => isset($items[$delta]->add_infos) ? unserialize($items[$delta]->add_infos) : NULL,
'#options' => $options_add_infos,
];
$element['columns_display'] = [
'#type' => 'select',
'#title' => t('Number of columnscolumn to display'),
'#default_value' => isset($items[$delta]->columns_display) ? $items[$delta]->columns_display : NULL,
'#options' => [
'null' => '--- Choose column(s) to display ---',
'col-md-12'3' => '1 column',
'col-md-9'6' => '2 columns',
'col-md-6'9' => '3 columns',
'col-md-3'12' => '4 columns',
],
];
//setting default value to all fields from above
/*$childs$childs = Element::children($element);
foreach ($childs as $child) {
$element[$child]['#default_value'] = isset($items[$delta]->{$child}) ? $items[$delta]->{$child} : NULL;
}*/
return $element;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
if($values[0]['add_infos'] != '0'){
$values[0]['add_infos'] = serialize($values[0]['add_infos']);
}
return $values;
}
}
/**
* Plugin implementation of the 'resource_formatter' formatter.
*
* @FieldFormatter(
* id = "resource_formatter",
* label = @Translation("Resource default"),
* field_types = {
* "resource"
* }
* )
*/
class ResourceDefaultFormatter extends FormatterBase{
public function viewElements(FieldItemListInterface $items, $langcode) {
$config = \Drupal::config('resources.config');
$element = [];
foreach ($items as $delta => $item) {
$column_display = $item->columns_display;
$topics = 'topics=' . str_replace(' ', '+', $item->topic);
$materials = '&materials=' . str_replace(' ', '+', $item->material);
$add_info = '';
foreach(unserialize($item->add_infos) as $info){
$add_info .= $info . ',';
}
$additional_info = '&additional_info=' . str_replace(' ', '+', substr($add_info, 0, $item-1)>add_infos);
$json_all = file_get_contents($config->get('global_config.url_api').'/all' . '?' . $topics . $materials . $additional_info );
$all_content = Json::decode($json_all);
if($all_content['pager']['total_items'] == 0){
$element[] = [
'#theme' => 'no_result',
];
} else {
foreach ($all_content['rows'] as $key => $content){
$type_explode = explode('-', $content['type']);
$type = strtolower(str_replace(' ', '_', trim(end($type_explode))));
switch($type){
case ('remote_videos'):
$element[] = [
'#theme' => 'video',
'#resource' => $content,
'#column_display' => $column_display
];
break;
default:
$element[] = [
'#theme' => 'file',
'#resource' => $content,
'#column_display' => $column_display
];
}
}
}
}
return $element;
}
}