created home node and some computed field
This commit is contained in:
@@ -3,5 +3,11 @@
|
||||
use Drupal\Core\Url;
|
||||
|
||||
function template_preprocess_materio_home(&$vars){
|
||||
$node_view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
|
||||
|
||||
$vm = "default";
|
||||
$fpnode = $vars['frontpage_node'];
|
||||
$nvb_fpnode = $node_view_builder->view($fpnode, $vm);
|
||||
$vars['frontpage_node'] = $nvb_fpnode;
|
||||
|
||||
}
|
||||
|
@@ -1,16 +1,113 @@
|
||||
<?php
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\mymodule\Plugin\Field\FieldType\MyFieldComputed;
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function materio_home_theme($existing, $type, $theme, $path) {
|
||||
// @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
|
||||
// function materio_home_theme($existing, $type, $theme, $path) {
|
||||
// // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
|
||||
//
|
||||
// return array(
|
||||
// 'materio_home' => array(
|
||||
// // 'render element' => '',
|
||||
// 'file' => 'includes/materio_home.inc',
|
||||
// 'variables' => array(
|
||||
// 'frontpage_node' => NULL,
|
||||
// ),
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
|
||||
return array(
|
||||
'materio_home' => array(
|
||||
// 'render element' => '',
|
||||
'file' => 'includes/materio_home.inc',
|
||||
'variables' => array(),
|
||||
),
|
||||
);
|
||||
/**
|
||||
* Implement hook_entity_bundle_field_info().
|
||||
*
|
||||
* @param EntityTypeInterface $entity_type
|
||||
* @param $bundle
|
||||
* @param array $base_field_definitions
|
||||
* @return array
|
||||
*/
|
||||
// function materio_home_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
|
||||
function materio_home_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
|
||||
// $fields = array();
|
||||
// if ($entity_type->id() == 'node' && $bundle === 'frontpage') {
|
||||
if ($entity_type->id() == 'node') {
|
||||
$fields['computed_materials_reference'] = BaseFieldDefinition::create('entity_reference')
|
||||
->setName('computed_materials_reference')
|
||||
->setLabel(t('Computed Materials References'))
|
||||
->setDescription(t('Computed Materials References.'))
|
||||
// // The Entity Type this field belongs to.
|
||||
->setSetting('target_type', 'node')
|
||||
// // The Entity Type bundle this field belongs to.
|
||||
->setTargetBundle('frontpage')
|
||||
->setTargetEntityTypeId('node')
|
||||
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
|
||||
->setComputed(TRUE)
|
||||
->setRevisionable(FALSE)
|
||||
->setTranslatable(FALSE)
|
||||
->setDisplayConfigurable('view', TRUE)
|
||||
->setDisplayOptions('view', [
|
||||
'label' => 'hidden',
|
||||
'weight' => -5,
|
||||
])
|
||||
->setClass(\Drupal\materio_home\Plugin\Field\FieldType\ComputedMaterialsReferences::class);
|
||||
|
||||
$fields['computed_showrooms_reference'] = BaseFieldDefinition::create('entity_reference')
|
||||
->setName('computed_showrooms_reference')
|
||||
->setLabel(t('Computed Showrooms References'))
|
||||
->setDescription(t('Computed Showrooms References.'))
|
||||
->setSetting('target_type', 'taxonomy_term')
|
||||
// // The Entity Type this field belongs to.
|
||||
->setTargetEntityTypeId('node')
|
||||
// // The Entity Type bundle this field belongs to.
|
||||
->setTargetBundle('frontpage')
|
||||
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
|
||||
->setComputed(TRUE)
|
||||
->setRevisionable(FALSE)
|
||||
->setTranslatable(FALSE)
|
||||
->setDisplayConfigurable('view', TRUE)
|
||||
->setDisplayOptions('view', [
|
||||
'label' => 'hidden',
|
||||
'weight' => -5,
|
||||
])
|
||||
->setClass(\Drupal\materio_home\Plugin\Field\FieldType\ComputedShowroomsReferences::class);
|
||||
}
|
||||
return $fields;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * implements hook_entity_extra_field_info
|
||||
// *
|
||||
// */
|
||||
// function materio_home_entity_extra_field_info() {
|
||||
// $extra = [];
|
||||
// // $extra['node']['frontpage']['form']['computed_materials_reference'] = [
|
||||
// // 'label' => t('Computed Materials References'),
|
||||
// // 'description' => t('Computed Materials References'),
|
||||
// // 'weight' => 10,
|
||||
// // ];
|
||||
// // $extra['node']['frontpage']['display']['computed_materials_reference'] = [
|
||||
// // 'label' => t('Computed Materials References'),
|
||||
// // 'description' => t('Computed Materials References'),
|
||||
// // 'weight' => 10,
|
||||
// // ];
|
||||
// return $extra;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Implements hook_install().
|
||||
*/
|
||||
function materio_home_install() {
|
||||
$entity_type = \Drupal::service('entity_type.manager')->getDefinition('node');
|
||||
\Drupal::service('entity.definition_update_manager')->updateEntityType($entity_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_uninstall().
|
||||
*/
|
||||
function materio_home_uninstall() {
|
||||
$entity_type = \Drupal::service('entity_type.manager')->getDefinition('node');
|
||||
\Drupal::service('entity.definition_update_manager')->updateEntityType($entity_type);
|
||||
}
|
||||
|
@@ -1,10 +1,10 @@
|
||||
materio_home.home:
|
||||
path: '/home'
|
||||
defaults:
|
||||
_controller: '\Drupal\materio_home\Controller\HomeController::home'
|
||||
_title: 'Home'
|
||||
requirements:
|
||||
_permission: 'access content'
|
||||
# materio_home.home:
|
||||
# path: '/home'
|
||||
# defaults:
|
||||
# _controller: '\Drupal\materio_home\Controller\HomeController::home'
|
||||
# _title: 'Home'
|
||||
# requirements:
|
||||
# _permission: 'access content'
|
||||
|
||||
# materio_home.home_mobile:
|
||||
# path: '/home_m'
|
||||
|
@@ -28,6 +28,15 @@ class HomeController extends ControllerBase {
|
||||
|
||||
$contents = array("#theme"=>'materio_home');
|
||||
|
||||
// presentation
|
||||
$query = \Drupal::entityQuery('node')
|
||||
->condition('status', 1)
|
||||
->condition('nid', 19990);
|
||||
// TODO: présentation nid should be a setting
|
||||
|
||||
$pres_nid = $query->execute();
|
||||
$contents["#frontpage_node"] = entity_load('node', array_pop($pres_nid));
|
||||
|
||||
return $contents;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\materio_home\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||
use Drupal\Core\Field\EntityReferenceFieldItemList;
|
||||
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
|
||||
use Drupal\Core\Field\FieldItemList;
|
||||
use Drupal\Core\TypedData\TypedDataInterface;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\Core\TypedData\ComputedItemListTrait;
|
||||
|
||||
// https://www.drupal.org/node/2112677
|
||||
// https://www.cornel.co/article/entity-reference-computed-field-example-drupal
|
||||
// https://www.caxy.com/blog/drupal-custom-form-and-computed-fields
|
||||
|
||||
class ComputedMaterialsReferences extends EntityReferenceFieldItemList
|
||||
{
|
||||
use ComputedItemListTrait;
|
||||
|
||||
/**
|
||||
* The entity type manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||||
*/
|
||||
protected $entityTypeManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(BaseFieldDefinition $definition, $name, TypedDataInterface $parent) {
|
||||
parent::__construct($definition, $name, $parent);
|
||||
$this->entityTypeManager = \Drupal::entityTypeManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the values.
|
||||
*/
|
||||
protected function computeValue() {
|
||||
$query = \Drupal::entityQuery('node')
|
||||
->condition('status', 1)
|
||||
->condition('type', 'materiau')
|
||||
->sort('created', 'DESC')
|
||||
->range(0,200);
|
||||
$results = $query->execute();
|
||||
$nids = array_rand($results, 20);
|
||||
$nodes = entity_load_multiple('node', $nids);
|
||||
// \Drupal::logger('materio_home')->notice(print_r($nodes, true));
|
||||
$key = 0;
|
||||
foreach ($nodes as $nid => $node) {
|
||||
// \Drupal::logger('materio_home')->notice($nid);
|
||||
$this->list[$key] = $this->createItem($key, $node->id());
|
||||
$key++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
namespace Drupal\materio_home\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||
use Drupal\Core\Field\EntityReferenceFieldItemList;
|
||||
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
|
||||
use Drupal\Core\Field\FieldItemList;
|
||||
use Drupal\Core\TypedData\TypedDataInterface;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\Core\TypedData\ComputedItemListTrait;
|
||||
|
||||
// https://www.drupal.org/node/2112677
|
||||
// https://www.cornel.co/article/entity-reference-computed-field-example-drupal
|
||||
// https://www.caxy.com/blog/drupal-custom-form-and-computed-fields
|
||||
|
||||
class ComputedShowroomsReferences extends EntityReferenceFieldItemList
|
||||
{
|
||||
use ComputedItemListTrait;
|
||||
/**
|
||||
* The entity type manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||||
*/
|
||||
protected $entityTypeManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(BaseFieldDefinition $definition, $name, TypedDataInterface $parent) {
|
||||
parent::__construct($definition, $name, $parent);
|
||||
$this->entityTypeManager = \Drupal::entityTypeManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the values.
|
||||
*/
|
||||
protected function computeValue() {
|
||||
$query = \Drupal::entityQuery('taxonomy_term')
|
||||
->condition('status', 1)
|
||||
->condition('vid', 'showroom');
|
||||
$tids = $query->execute();
|
||||
shuffle($tids);
|
||||
$terms = entity_load_multiple('taxonomy_term', $tids);
|
||||
// \Drupal::logger('materio_home')->notice(print_r($nodes, true));
|
||||
$key = 0;
|
||||
foreach ($terms as $tid => $term) {
|
||||
// \Drupal::logger('materio_home')->notice($nid);
|
||||
$this->list[$key] = $this->createItem($key, $term->id());
|
||||
$key++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1 +1 @@
|
||||
Hello Materio :)
|
||||
{{ frontpage_node }}
|
||||
|
Reference in New Issue
Block a user