OctoberCMS get list of content files - php

Trying to get content files started with cont*
using :
Content::loadCached('theme', 'listOfContentFiles');
And getting an error.
I can get one but not the list.

Seems there is no direct way of doing it, you can use this code to get list manually and filter it by your self
use Cms\Classes\Content;
use Cms\Classes\Theme;
$activeTheme = Theme::getActiveTheme();
$instance = Content::inTheme($activeTheme);
$items = $instance->newQuery()->lists('fileName');
$loadedItems = [];
foreach ($items as $item) {
// we need to manually filter data you can
// add more logic here for sub directory parsing etc
if(starts_with($item, 'cont_')) {
$loadedItems[] = Content::loadCached($activeTheme, $item);
}
}
dd($loadedItems);
// if you want to make it collection
$result = $instance->newCollection($loadedItems);
it will return you list of content files in active theme by our filter logic.

Related

how to use every item in the loop

I'm created a new script with php. I manipulate the picture uploaded by the user with 40 different models and show them in the page.
The user has to wait until all 40 pictures are prepared with my current code.
$file = $this->post['uploaded_image'];
$models = $this->post['models'];
/*
User selected models, etc:
['model-1', 'model-2', 'model-8', 'model-14', 'model-22', 'model-34', 'model-35', 'model-40']
*/
foreach ($models as $model) {
$resize_image = $this->resize($file, $model);
$final_image = $this->crop($resize_image, $model, $file);
return $final_image;
}
I want to do is to use each image that return from the loop in order. While I use the first image and the loop continues to render in the 2nd, 3rd...
I hope I explained my problem correctly. How can I solve this problem with php or javascript?

Site map generator in laravel 5 with RoumenDianoff sitemap

Im implementing the laravel RoumenDianoff Sitemap in my application
https://github.com/RoumenDamianoff/laravel-sitemap/wiki/Dynamic-sitemap
And im really confused on this part of the code
Route::get('sitemap', function(){
// create new sitemap object
$sitemap = App::make("sitemap");
// set cache key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean)
$sitemap->setCache('laravel.sitemap', 60);
$posts = DB::table('posts')->orderBy('created_at', 'desc')->get();
foreach ($posts as $post)
{
$sitemap->add($post->slug, $post->modified, $post->priority, $post->freq);
}
As I understand i'm creating a route to use sitemap as that function so the part I don't get is when do I iterate trough every link of my website, or how do i get this links from my website to add then to the for each on that function, i mean it looks like that $posts variable but I don't have record of my links on any database so how can i get this links.
What I would do is generate the links dynamically (probably using values from the database). Then create a sitemap object that will be populated with all the links (use foreach loop) and finally store this. Check out the code fragment below:
Route::get('sitemap',function(){
// Generate your links dynamically here
$link = [];
$locations = DB::locations(); /** some database values **/
foreach($locations as $location){
$link = route('home') . '/shoes-for-sale-in-' . strtolower($location);
// create new sitemap object
$sitemap = \App::make("sitemap");
// Add link, priority,last modified and change frequency
$sitemap->add($link, null, 0.5, 'monthly');
}
// generate sitemap (format, filename)
$sitemap->store('xml', 'commercial');
});

Trying to highlight current menu item in Drupal 8 using hook_preprocess_menu

I have the below function to create active trail functionality. So if I were to have /blog as a "parent" and a post of /blog/mypost, when on mypost the blog link would show as highlighted. I don't want to have to make menu items for all the blog posts. The problem is when caching is turned on (not using settings.local.php and debug turned off) the getRequestUri isn't changing on some pages. It seems to be cached depending on the page. It works fine with page caching turned off but I'd like to get this working with caching. Is there a better way to check for the current path and apply the active class?
function mytheme_preprocess_menu(&$variables, $hook) {
if($variables['theme_hook_original'] == 'menu__main'){
$node = \Drupal::routeMatch()->getParameter('node');
if($node){
$current_path = \Drupal::request()->getRequestUri();
$items = $variables['items'];
foreach ($items as $key => $item) {
// If current path starts with a part of another path i.e. a parent, set active to li.
if (0 === strpos($current_path, $item['url']->toString())) {
// Add active link.
$variables['items'][$key]['attributes']['class'] .= ' menu-item--active-trail';
}
}
}
}
}
I've also tried putting this into a module to try and see if I can get the current path to then do the twig logic in the menu--main.twig.html template but I have the same problem.
function highlight_menu_sections_template_preprocess_default_variables_alter(&$variables) {
$variables['current_path'] = $_SERVER['REQUEST_URI'];
}
After a very long time trying all sorts of things, I found an excellent module which addresses exactly this problem. Install and go, not configuration, it just works:
https://www.drupal.org/project/menu_trail_by_path
Stable versions for D7 and D8.
I tried declaring an active path as part of a custom menu block, and even then my declared trail gets cached. Assuming it's related to the "There is no way to set the active link - override the service if you need more control." statement in this changelog, though why MenuTreeParameters->setActiveTrail() exists is anybody's guess.
For the curious (and for me when I search for this later!), here's my block's build() function:
public function build() {
$menu_tree = \Drupal::menuTree();
$parameters = new MenuTreeParameters();
$parameters->setRoot('menu_link_content:700c69e6-785b-4db7-be49-73188b47b5a3')->setMinDepth(1)->setMaxDepth(1)->onlyEnabledLinks();
// An array of routes and menu_link_content ids to set as active
$define_active_mlid = array(
'view.press_releases.page_1' => 385
);
$route_name = \Drupal::request()->get(RouteObjectInterface::ROUTE_NAME);
if (array_key_exists($route_name, $define_active_mlid)) {
$menu_link = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(array('id' => $define_active_mlid[$route_name]));
$link = array_shift($menu_link);
$parameters->setActiveTrail(array('menu_link_content:' . $link->uuid()));
}
$footer_tree = $menu_tree->load('footer', $parameters);
$manipulators = array(
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$tree = $menu_tree->transform($footer_tree, $manipulators);
$menu = $menu_tree->build($tree);
return array(
'menu' => $menu,
);
}
[adding a new answer since this is a completely different approach than my earlier one]
If a CSS-based solution is acceptable, this seems to work okay:
.page-node-type-press-release {
a[data-drupal-link-system-path="press-room/press-releases"] {
// active CSS styles here
}
}

Drupal 7 Render a Comment Object

So this is the problem I am running into. If I have a comment object, I want to create a renderable array that is using the display settings of that comment. As of now this is what I have:
$commentNew = comment_load($var);
$reply[] = field_view_value('comment', $commentNew, 'comment_body', $commentNew->comment_body['und'][0]);
Which works fine because I dont have any specific settings setup for the body. But I also have image fields and video embed fields that I need to have rendered the way they are setup in the system. How would I go about doing that?
Drupal core does it with the comment_view() function:
$comment = comment_load($var);
$node = node_load($comment->nid);
$view_mode = 'full'; // Or whatever view mode is appropriate
$build = comment_view($comment, $node, $view_mode);
If you need to change a particular field from the default, use hook_comment_view():
function MYMODULE_comment_view($comment, $view_mode, $langcode) {
$comment->content['body'] = array('#markup' => 'something');
}
or just edit the $build array received from comment_view() as you need to if implementing the hook won't work for your use case.

Magento programmatically creating a sales/quote_address object

I am having an issue with creating and adding sales/quote_address objects to the multishipping checkout process. Currently, whenever I create a new address object, it is just reusing the exact same object over and over; Thus, when I add items to one address, it will add them to all addresses. As a check, I put a for loop after my main loop to echo out all of the ID's of the created addresses - it always echos out the number 3. When i try to dynamically change the ID of the newly created addresses(commented out section) they won't even show up at all in the final for loop. My code is as follows:
//dynamically create the addresses and add them to the shipping information screen
$idCounter = 1;
foreach($dropshippersCombineWProducts as $dropshippersWCProducts) {
$newAddress = null;
$newAddress = Mage::getSingleton('sales/quote_address')->importCustomerAddress($customAddress);
//$idCounter++;
//$newAddress->setId($idCounter);
foreach ($newAddress->getItemsCollection() as $item) {
$item->isDeleted(true);
}
foreach ($dropshippersWCProducts[1] as $_item) {
$newAddress->addItem($_item);
}
$quote->setShippingAddress($newAddress);
$newAddress->collectShippingRates();
}
$addresses = $quote->getAllShippingAddresses();
foreach ($addresses as $address) {
echo $address->getId();
}
Any help would be much appreciated.
The getSingleton method always returns the same object instance. Try changing that call to getModel and see if it doesn't fix the problem for you.

Categories