diff --git a/src/Plugin/Block/UwCblManualList.php b/src/Plugin/Block/UwCblManualList.php index 65be2b9718c24380c8c374c61871d44747465c53..bc7116fe6fb34b42375e9b91f74b89055fc9e064 100644 --- a/src/Plugin/Block/UwCblManualList.php +++ b/src/Plugin/Block/UwCblManualList.php @@ -3,6 +3,7 @@ namespace Drupal\uw_custom_blocks\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\uw_custom_blocks\Service\UwBlockManualElements; @@ -49,6 +50,13 @@ class UwCblManualList extends BlockBase implements ContainerFactoryPluginInterfa */ protected $uwBlockManualRender; + /** + * Entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + /** * Constructs a BlockComponentRenderArray object. * @@ -66,6 +74,8 @@ class UwCblManualList extends BlockBase implements ContainerFactoryPluginInterfa * The manual block get config. * @param \Drupal\uw_custom_blocks\Service\UwBlockManualRender $uwBlockManualRender * The manual block render array. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * The entity type manager. */ public function __construct( array $configuration, @@ -74,7 +84,8 @@ class UwCblManualList extends BlockBase implements ContainerFactoryPluginInterfa UwBlockService $uwBlockService, UwBlockManualElements $uwBlockManualElements, UwBlockManualGetConfig $uwBlockManualGetConfig, - UwBlockManualRender $uwBlockManualRender + UwBlockManualRender $uwBlockManualRender, + EntityTypeManagerInterface $entityTypeManager ) { parent::__construct($configuration, $plugin_id, $plugin_definition); @@ -82,6 +93,7 @@ class UwCblManualList extends BlockBase implements ContainerFactoryPluginInterfa $this->uwBlockManualElements = $uwBlockManualElements; $this->uwBlockManualGetConfig = $uwBlockManualGetConfig; $this->uwBlockManualRender = $uwBlockManualRender; + $this->entityTypeManager = $entityTypeManager; } /** @@ -101,7 +113,8 @@ class UwCblManualList extends BlockBase implements ContainerFactoryPluginInterfa $container->get('uw_custom_blocks.uw_block_service'), $container->get('uw_custom_blocks.uw_block_manual_elements'), $container->get('uw_custom_blocks.uw_block_manual_get_config'), - $container->get('uw_custom_blocks.uw_block_manual_render') + $container->get('uw_custom_blocks.uw_block_manual_render'), + $container->get('entity_type.manager') ); } @@ -191,6 +204,70 @@ class UwCblManualList extends BlockBase implements ContainerFactoryPluginInterfa // Get the content type from the values. $content_type = $values['content_type']; + // Put all the items into an array using the weight. + // If there is more than one in a given weight, we will + // sort by the title later. + foreach ($values[$content_type]['items_fieldset'][$content_type . '_ids'] as $value) { + $weight_items[$value['weight']][] = $value['id']; + } + + // Counter to be used for weight. + $counter = 0; + + // Step through all the weighted items and check if + // there is more than one, and if so, sort by title. + foreach ($weight_items as $weight_item) { + + // If there is more than one in a given weight, then step + // through, get all the titles, then sort that array. + if (count($weight_item) > 1) { + + // Reset the sorted items array. + $sorted_items = []; + + // Step through each in the weight and get the title + // from the node. + foreach ($weight_item as $wi) { + $node = $this->entityTypeManager + ->getStorage('node') + ->load($wi); + $sorted_items[$wi] = $node->getTitle(); + } + + // Sort the array by the title, keeping the indexes. + asort($sorted_items); + + // Now that the given weight is sorted by title, we can + // simply add to an array. + foreach (array_keys($sorted_items) as $key) { + + // Add the item to the array. + $items_to_use[] = [ + 'id' => $key, + 'weight' => $counter, + ]; + + // Increment the counter so we have the correct index. + $counter++; + } + } + else { + + // If we reach here there is only one item in the given weight, + // so we can simply add it to the array. + $items_to_use[] = [ + 'id' => $weight_item[0], + 'weight' => $counter, + ]; + + // Increment the counter, so we get the correct index. + $counter++; + } + } + + // Now replace the items with the sorted by weight and title array. + $values[$content_type]['items_fieldset'][$content_type . '_ids'] = $items_to_use; + // Set the content type and heading level // in the block config. $this->configuration['content_type'] = $content_type;