lokomotivan
10/17/2017 - 11:20 AM

Processwire Snippets

Processwire Snippets

<?php
// Dynamic js/css files
foreach($config->styles->unique() as $file) {
  echo "<link rel='stylesheet' type='text/css' href='$file' />";
}
foreach($config->scripts->unique() as $file) {
  echo "<script type='text/javascript' src='$file'></script>";
}

/**
 * sessionExpireSeconds: how many seconds of inactivity before session expires
 * (site/config.php)  
 * 
 */
$config->sessionExpireSeconds = 86400;


// find repeater items like pages, template always starts as "reapeater_"
$repeater_items = $pages->find("template=repeater_FIELD_NAME, featured=1");

// GEt option field values
$get_filed = $fields->get('some_field_name');
$field_options = $get_filed->type->getOptions($get_filed);

// get random image
$image = $page->images->getRandom();

// get module config value
$some_module = $modules->get("ModuleClassHere");
$some_module->some_field_config;

// redirect
$pages->addHookAfter('Page::path', null, 'hookPagePath');
function hookPagePath(HookEvent $e) {
 	$page = $e->object;
 	if($page->template == 'blog-post') $e->return = "/blog/$page->name/";
}

/**
 * Check if current page i admin page
 *
 */
$isAdmin = $this->page->rootParent->id == 2;
// in init
if(strpos($_SERVER['REQUEST_URI'], $this->wire('config')->urls->admin) === 0) {
  // its admin page, do someting
}

/**
 *  Convert WireArray to php Array
 */
// array with all keys and values
$array = $wireArray->getArray();

// array of single key value
$array = $wireArray->explode("id");

/*
 * GEt Reapater next/prev item
 */
foreach($widget->items as $item) {
     // items counter
    $c = $i++;
    $n = $c + 1;
    $p = $c - 1;
    $nextItem = $widget->items[$n];
    $prevItem = $widget->items[$p];
    
    echo $nextItem->title;
    echo $prevItem->title;
}
<?php

    // page created date
    $page_date    = date('F j, Y', $page->created);

    // current date timestam for calculations
    $current_date   = time();
    // curent date
    $today_date     = date('F j, Y');
    // today + 30 days
    $expire_date    = date('F j, Y', strtotime("+30 days"));

    // expire check example
    if($current_date > $expire_date) {
        // do something
    }

    // calculate diference between dates in days
    $register_date  = $u->created;
    $current_date   = time();
    $diference_date = $register_date - $current_date;
    $numb_of_days  = floor($diference_date / (60 * 60 * 24));
<?php
/**
 *
 *    Create blank 'style.less' file in '/templates/less/' folder
 *    include style.less in theme.less just after vars.less
 *    run code below to update style.less file and override variables
 *
 */
 
 // file path
$filePath = $config->paths->templates."less/style.less";

// open file for writing
$file = fopen($filePath,"w");

// write variables to file
return fwrite($file, "
	@tm-primary-bg: blue;
	@tm-secondary-bg: red;
	@tm-some-bg: blue;
");

// close file
fclose($file);
$(document).ready(function() {
  
    $('#some_id').on('click', function(e) {

        $.post(
            '<?=$pages->get('/graphql/')->url?>', // this is the url where your GraphQL api is exposed.
            {
                query: `{ model { list { id, title } } }` // this is query
            },
            function (result) {
                 // do saomething with feched data
                console.log(result);
            }
        )
        .done(function() {
            // do something when done
            console.log("Done!");
        });

    });
  
});