BIG REFACTORING using composer create-project drupal-composer/drupal-project

This commit is contained in:
2019-01-28 17:05:29 +01:00
parent 17bb6e1dbc
commit 37689c3d4b
504 changed files with 1740 additions and 628 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' => 'span',
'#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;
}
}