Frontend\Core\Engine\Block\Widget::getForId PHP Метод

getForId() публичный статический Метод

public static getForId ( Symfony\Component\HttpKernel\KernelInterface $kernel, string $module, string $action, integer | null $id = null ) : string | null
$kernel Symfony\Component\HttpKernel\KernelInterface
$module string The module to load.
$action string The action to load.
$id integer | null This is not the modules_extra id but the id of the item itself
Результат string | null if we have data it is still serialised since it will be unserialized in the constructor
    public static function getForId(KernelInterface $kernel, $module, $action, $id = null)
    {
        $query = 'SELECT data FROM modules_extras WHERE type = :widget AND module = :module AND action = :action';
        $parameters = ['widget' => 'widget', 'module' => $module, 'action' => $action];
        if (is_numeric($id)) {
            $query .= ' AND data LIKE :data';
            $parameters['data'] = '%s:2:"id";i:' . (int) $id . ';%';
        }
        return new self($kernel, $module, $action, $kernel->getContainer()->get('database')->getVar($query, $parameters));
    }

Usage Example

Пример #1
0
 /**
  * Parse a widget straight from the template, rather than adding it through pages.
  *    syntax: {{ parsewidget($module, $action, $id) }}
  *
  * @internal if your widget outputs random data you should cache it inside the widget
  * Fork checks the output and if the output of the widget is random it will loop until the random data
  * is the same as in the previous iteration
  *
  * @param string $module The module whose module we want to execute.
  * @param string $action The action to execute.
  * @param string $id     The widget id (saved in data-column).
  *
  * @return null|string
  * @throws Exception
  */
 public static function parseWidget($module, $action, $id = null)
 {
     // create new widget instance and return parsed content
     $extra = FrontendBlockWidget::getForId(FrontendModel::get('kernel'), $module, $action, $id);
     // set parseWidget because we will need it to skip setting headers in the display
     FrontendModel::getContainer()->set('parseWidget', true);
     try {
         $extra->execute();
         $content = $extra->getContent();
         FrontendModel::getContainer()->set('parseWidget', null);
         return $content;
     } catch (Exception $e) {
         // if we are debugging, we want to see the exception
         if (FrontendModel::getContainer()->getParameter('kernel.debug')) {
             throw $e;
         }
         return;
     }
 }