materio_samples is working beta

This commit is contained in:
Bachir Soussi Chiadmi 2018-12-18 16:57:24 +01:00
parent b8f572a83e
commit 73f521ec9d
22 changed files with 672 additions and 13 deletions

View File

@ -0,0 +1,7 @@
field.field_settings.materio_samples_field:
type: mapping
label: 'Samples settings'
mapping:
vid:
type: string
label: 'Vocabulary'

View File

@ -0,0 +1,11 @@
name: Materio Samples
type: module
description: "Provide a sample field, each showroom (taxonomy term) can fill it's own sample reference, and only it's own"
core: 8.x
package: Materio
dependencies:
- taxonomy
- user
# config_devel:
# install:

View File

@ -0,0 +1,57 @@
<?php
namespace Drupal\materio_samples\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\taxonomy\Entity\Term;
/**
* Plugin implementation of the 'materio_samples_default_formatter' formatter.
*
* @FieldFormatter(
* id = "materio_samples_default_formatter",
* module = "materio_samples",
* label = @Translation("Simple list key paired formatter"),
* field_types = {
* "materio_samples_field"
* }
* )
*/
class SamplesDefaultFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$elements = [];
foreach ($items as $delta => $item) {
// return nothing if target_id is null
if(!$item->target_id) return;
$term = Term::load($item->target_id);
// translate the term
$term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language);
$elements[$delta] = [
// We create a render array to produce the desired markup,
// "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
// See theme_html_tag().
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this->t('@target : @location', [
'@target' => $term->getName(),
'@location' => $item->location
]
),
];
}
return $elements;
}
}

View File

@ -0,0 +1,125 @@
<?php
namespace Drupal\materio_samples\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\DataReferenceTargetDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Plugin implementation of the 'field_example_rgb' field type.
*
* @FieldType(
* id = "materio_samples_field",
* label = @Translation("Samples"),
* module = "materio_samples",
* category = "Materio",
* description = @Translation("Provide a sample field, each showroom (taxonomy term) can fill it's own sample reference, and only it's own."),
* default_widget = "materio_samples_default_widget",
* default_formatter = "materio_samples_default_formatter"
* )
*/
class SamplesItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'location' => [
'description' => "The actual location reference in the showroom.",
'type' => 'text',
'size' => 'tiny',
// 'not null' => FALSE,
],
'target_id' => [
'description' => 'the id of the target taxonomy term.',
'type' => 'int',
'unsigned' => TRUE
]
],
'indexes' => [
'target_id' => ['target_id'],
],
];
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('location')->getValue();
return $value === NULL || $value === '';
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['location'] = DataDefinition::create('string')
->setLabel(t('Reference'));
$properties['target_id'] = DataReferenceTargetDefinition::create('integer')
// ->setLabel(new TranslatableMarkup('@label ID', ['@label' => $target_type_info->getLabel()]))
->setLabel(new TranslatableMarkup('@label ID', ['@label' => 'Showroom'])) // get voc name from settings
->setSetting('unsigned', TRUE)
->setRequired(TRUE);
return $properties;
}
/**
* {@inheritdoc}
*/
public static function defaultFieldSettings() {
return [
// Declare a single setting, 'size', with a default
// value of 'large'
'vid' => null,
] + parent::defaultFieldSettings();
}
/**
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
// dsm($form);
// get vocabularies
$vocabularies = \Drupal\taxonomy\Entity\Vocabulary::loadMultiple();
// dsm($vocabularies);
$options = [null => "choose"];
foreach ($vocabularies as $vid => $voc) {
$options[$voc->id()] = $voc->label();
}
// dsm($options);
$element = [];
// The key of the element should be the setting name
$element['vid'] = [
'#title' => $this->t('Vocabulary'),
'#type' => 'select',
'#options' => $options,
'#default_value' => $this->getSetting('vid'),
];
return $element;
}
/**
* Form element validation handler; Invokes selection plugin's validation.
*
* @param array $form
* The form where the settings form is being included in.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state of the (entire) configuration form.
*/
public static function fieldSettingsFormValidate(array $form, FormStateInterface $form_state) {
$field = $form_state->getFormObject()->getEntity();
dsm($field);
}
}

View File

@ -0,0 +1,131 @@
<?php
namespace Drupal\materio_samples\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\workflow\Entity\WorkflowManager;
/**
* Plugin implementation of the 'materio_samples_default_widget' widget.
*
* @FieldWidget(
* id = "materio_samples_default_widget",
* module = "materio_samples",
* label = @Translation("Samples"),
* field_types = {
* "materio_samples_field"
* }
* )
*/
class SamplesDefaultWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL) {
// construct "manually" the list of items
$vid = $this->fieldDefinition->getSetting('vid');
$query = \Drupal::entityQuery('taxonomy_term')
->sort('weight', 'DESC')
// ->sort('tid', 'DESC')
->condition('vid', $vid);
$tids = $query->execute();
$terms = Term::loadMultiple($tids);
// define the cardinality, this will remove the add_more btn
$this->fieldDefinition->getFieldStorageDefinition()->setCardinality(count($terms));
$locations = [];
foreach ($items as $delta => $item) {
$locations[$item->target_id] = $item->location;
}
$delta = 0;
foreach ($terms as $term) {
// remove masqué
$sid = WorkflowManager::getCurrentStateId($term, 'field_workflow');
if($sid == 'workflow_hidden') continue;
$location = isset($locations[$term->id()]) ? $locations[$term->id()] : '';
$value = array(
'location' => $location,
'target_id'=> $term->id()
);
$items->set($delta, $value);
$delta ++;
}
// then call the normal form
$elements = parent::form($items, $form, $form_state, $get_delta);
// dsm($elements);
// Arrange the form object to remove draggable table stuff
$elements['widget']['#cardinality_multiple'] = FALSE;
for ($i=0; $i <= $delta ; $i++) {
if(isset($elements['widget'][$i]['_weight'])){
$elements['widget'][$i]['_weight']['#type'] = 'hidden';
}
}
return $elements;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
// get default values
$target_id = isset($items[$delta]->target_id) ? $items[$delta]->target_id : 0;
$location = isset($items[$delta]->location) ? $items[$delta]->location : '';
// dsm($target_id);
// return nothing if target_id is null
if(!$target_id) return;
$term = Term::load($target_id);
// translate the term
$term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language);
// dsm($element);
// $element['#attributes'] = array('class' => array('container-inline'));
// $element['container'] = [
// '#type' => 'container',
// '#field_prefix' => '<div class="container-inline">',
// '#field_suffix' => '</div>',
// ];
$element['target_id'] = [
'#type' => 'hidden',
'#default_value' => $target_id,
];
$element['location'] = [
'#title' => $term->getName(),
'#type' => 'textfield',
'#default_value' => $location,
'#size' => 10,
'#maxlength' => 15,
'#attributes' => ['class' => ['container-inline']],
];
// return ['value' => $element];
return $element;
}
public function validate($element, FormStateInterface $form_state) {
// dsm($element);
}
/**
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
// foreach ($values as $key => $value) {
// $values[$key]['value']['target_id'] = (int)$values[$key]['value']['target_id'];
// }
dsm($values);
return $values;
}
}

View File

@ -15,6 +15,7 @@ dependencies:
- field.field.node.materiau.field_memo
- field.field.node.materiau.field_migration
- field.field.node.materiau.field_reference
- field.field.node.materiau.field_samples
- field.field.node.materiau.field_short_description
- field.field.node.materiau.field_tags
- field.field.node.materiau.field_thesaurus
@ -27,6 +28,7 @@ dependencies:
- field_group
- file
- image
- materio_samples
- text
- video_embed_field
- workflow
@ -42,6 +44,7 @@ third_party_settings:
- group_links
- group_samples_showroom
- group_internal
- group_samples
parent_name: ''
weight: 0
format_type: tabs
@ -135,7 +138,8 @@ third_party_settings:
required_fields: true
label: Links
group_samples_showroom:
children: { }
children:
- field_samples
parent_name: group_htabs
weight: 14
format_type: tab
@ -249,6 +253,12 @@ content:
third_party_settings: { }
type: workflow_default
region: content
field_samples:
weight: 34
settings: { }
third_party_settings: { }
type: materio_samples_default_widget
region: content
field_short_description:
weight: 2
settings:

View File

@ -15,6 +15,7 @@ dependencies:
- field.field.node.materiau.field_memo
- field.field.node.materiau.field_migration
- field.field.node.materiau.field_reference
- field.field.node.materiau.field_samples
- field.field.node.materiau.field_short_description
- field.field.node.materiau.field_tags
- field.field.node.materiau.field_thesaurus
@ -25,6 +26,7 @@ dependencies:
- computed_field
- file
- image
- materio_samples
- options
- text
- user
@ -132,6 +134,13 @@ content:
third_party_settings: { }
type: computed_string
region: content
field_samples:
weight: 118
label: above
settings: { }
third_party_settings: { }
type: materio_samples_default_formatter
region: content
field_short_description:
weight: 102
label: above

View File

@ -66,6 +66,7 @@ module:
mailsystem: 0
materio_id: 0
materio_migrate: 0
materio_samples: 0
matomo: 0
maxlength: 0
menu_block: 0

View File

@ -5,6 +5,6 @@ error_handlers:
rebuild_theme: false
debug_mail_file_format: '%to-%subject-%datetime.mail.txt'
debug_mail_directory: 'temporary://devel-mails'
devel_dumper: default
devel_dumper: kint
_core:
default_config_hash: 8SYa5OOpQGdg4wnttb0LFNuG6GmivsS2qNv9sTH9gDI

View File

@ -0,0 +1,22 @@
uuid: e1a0b688-5eeb-41c5-8b01-2bfd840d63fa
langcode: en
status: true
dependencies:
config:
- field.storage.node.field_samples
- node.type.materiau
module:
- materio_samples
id: node.materiau.field_samples
field_name: field_samples
entity_type: node
bundle: materiau
label: Samples
description: ''
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings:
vid: showroom
field_type: materio_samples_field

View File

@ -0,0 +1,19 @@
uuid: be56dc4b-a9d2-4f35-b7da-236d73b827a8
langcode: en
status: true
dependencies:
module:
- materio_samples
- node
id: node.field_samples
field_name: field_samples
entity_type: node
type: materio_samples_field
settings: { }
module: materio_samples
locked: false
cardinality: -1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false

View File

@ -1,4 +1,4 @@
uuid: a7bc7a67-a8ab-4d55-9a8f-f2e21e4e7145
uuid: 417ec5b7-cdec-4df7-bde3-06cf48ce0c6e
langcode: en
status: true
dependencies: { }

View File

@ -1,4 +1,4 @@
uuid: 0a46ed54-515f-40a0-8147-ac71d58c3f6c
uuid: 55e84e5f-c055-46a0-9b5d-9bcdd1a88040
langcode: en
status: true
dependencies: { }

View File

@ -1,9 +1,9 @@
uuid: 9ff8173a-d98d-44f3-ad8b-aa7fc344ffca
uuid: 2e27d7f8-a9af-4d74-835d-01f3145e86c5
langcode: en
status: true
dependencies: { }
_core:
default_config_hash: DnMjLkb9wXzqI2NMRiu9dKN5NujCtPmjLtvff6sz1Rk
default_config_hash: waCCDN5EkmNAqKfi7oNHtpYnqMRfal_zZrVCValRCM8
id: d7_node_materiau
class: null
field_plugin_method: null
@ -116,6 +116,9 @@ process:
source: fid
no_stub: true
description: description
field_famille: field_famille
field_index: field_identifiant
field_reference: field_reference_materio
destination:
plugin: 'entity:node'
migration_dependencies:

View File

@ -1,4 +1,4 @@
uuid: 97152dee-a197-455a-942b-35585342b022
uuid: 96cfa88f-d70d-4617-bbf8-ead8262c5579
langcode: en
status: true
dependencies: { }

View File

@ -1,4 +1,4 @@
uuid: ce9f81b5-4979-4f90-856a-a501f8e32d5e
uuid: 23aa0f8e-825c-4e48-bb71-c9b021638e87
langcode: en
status: true
dependencies: { }

View File

@ -1,4 +1,4 @@
uuid: ce7a00bb-f73b-468d-92ad-abde0e21f120
uuid: 9ad7f878-a7ae-4efa-8d00-96bf2a472b19
langcode: en
status: true
dependencies: { }

View File

@ -1,4 +1,4 @@
uuid: 0eb8dd85-169f-4096-9aba-da1292c7a3dd
uuid: 580e6065-b175-4985-9f9b-6a4a36ab1f75
langcode: en
status: true
dependencies: { }

View File

@ -1,4 +1,4 @@
uuid: fb77a05b-0bbc-4bba-aede-59b373ede799
uuid: 450375ec-780f-49c9-9afd-b63b71828cda
langcode: en
status: true
dependencies: { }

View File

@ -1,4 +1,4 @@
uuid: 1e6cd0f1-e949-4fac-b935-b26c09be445d
uuid: fd046a7f-6a3e-4787-be4e-6275cdda39fe
langcode: en
status: true
dependencies: { }

View File

@ -1,4 +1,4 @@
uuid: fb89ae32-f11e-4845-aaf4-476f3f295105
uuid: e678ea2d-f5b3-4b89-9e0a-a7a8b547f3e3
langcode: en
status: true
dependencies:

View File

@ -4,12 +4,15 @@ status: true
dependencies:
config:
- field.storage.node.field_distributor
- field.storage.node.field_famille
- field.storage.node.field_index
- field.storage.node.field_linked_breves
- field.storage.node.field_linked_materials
- field.storage.node.field_manufacturer
- field.storage.node.field_materiau_images
- field.storage.node.field_memo
- field.storage.node.field_migration
- field.storage.node.field_reference
- field.storage.node.field_tags
- field.storage.node.field_thesaurus
- field.storage.node.field_visuel
@ -19,6 +22,7 @@ dependencies:
- user.role.admin
- user.role.root
module:
- computed_field
- content_lock
- content_translation
- image
@ -102,6 +106,9 @@ display:
views_bulk_operations_bulk_form: views_bulk_operations_bulk_form
field_materiau_images: field_materiau_images
field_visuel: field_materiau_images
field_famille: field_famille
field_index: field_famille
field_reference: field_famille
nid: title
title: title
edit_node: title
@ -133,6 +140,27 @@ display:
separator: ''
empty_column: false
responsive: ''
field_famille:
sortable: false
default_sort_order: asc
align: ''
separator: '<br />'
empty_column: false
responsive: ''
field_index:
sortable: false
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
field_reference:
sortable: false
default_sort_order: asc
align: ''
separator: ''
empty_column: false
responsive: ''
nid:
sortable: false
default_sort_order: asc
@ -439,6 +467,199 @@ display:
separator: ', '
field_api_classes: false
plugin_id: field
field_famille:
id: field_famille
table: node__field_famille
field: field_famille
relationship: none
group_type: group
admin_label: ''
label: Famille
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: list_default
settings: { }
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
plugin_id: field
field_index:
id: field_index
table: node__field_index
field: field_index
relationship: none
group_type: group
admin_label: ''
label: Index
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: computed_integer
settings:
cache_unit: '-1'
cache_duration: '1'
thousand_separator: ''
prefix_suffix: 1
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
plugin_id: field
field_reference:
id: field_reference
table: node__field_reference
field: field_reference
relationship: none
group_type: group
admin_label: ''
label: Reference
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: computed_string
settings:
sanitized: 1
cache_unit: '-1'
cache_duration: '1'
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
plugin_id: field
nid:
id: nid
table: node_field_data
@ -1693,6 +1914,43 @@ display:
group_items: { }
reduce_duplicates: false
plugin_id: workflow_state
field_reference_value:
id: field_reference_value
table: node__field_reference
field: field_reference_value
relationship: none
group_type: group
admin_label: ''
operator: 'not empty'
value: ''
group: 1
exposed: false
expose:
operator_id: ''
label: ''
description: ''
use_operator: false
operator: ''
identifier: ''
required: false
remember: false
multiple: false
remember_roles:
authenticated: authenticated
placeholder: ''
is_grouped: false
group_info:
label: ''
description: ''
identifier: ''
optional: true
widget: select
multiple: false
remember: false
default_group: All
default_group_multiple: { }
group_items: { }
plugin_id: string
sorts:
created:
id: created
@ -1743,12 +2001,15 @@ display:
- user.roles
tags:
- 'config:field.storage.node.field_distributor'
- 'config:field.storage.node.field_famille'
- 'config:field.storage.node.field_index'
- 'config:field.storage.node.field_linked_breves'
- 'config:field.storage.node.field_linked_materials'
- 'config:field.storage.node.field_manufacturer'
- 'config:field.storage.node.field_materiau_images'
- 'config:field.storage.node.field_memo'
- 'config:field.storage.node.field_migration'
- 'config:field.storage.node.field_reference'
- 'config:field.storage.node.field_tags'
- 'config:field.storage.node.field_thesaurus'
- 'config:field.storage.node.field_visuel'
@ -1948,12 +2209,15 @@ display:
- user.roles
tags:
- 'config:field.storage.node.field_distributor'
- 'config:field.storage.node.field_famille'
- 'config:field.storage.node.field_index'
- 'config:field.storage.node.field_linked_breves'
- 'config:field.storage.node.field_linked_materials'
- 'config:field.storage.node.field_manufacturer'
- 'config:field.storage.node.field_materiau_images'
- 'config:field.storage.node.field_memo'
- 'config:field.storage.node.field_migration'
- 'config:field.storage.node.field_reference'
- 'config:field.storage.node.field_tags'
- 'config:field.storage.node.field_thesaurus'
- 'config:field.storage.node.field_visuel'