Back to ContainerAwareToolbarFactory class

Method createButton

public \Joomla\CMS\Toolbar\ToolbarButton
createButton
(\Joomla\CMS\Toolbar\Toolbar $toolbar, string $type)
Creates a new toolbar button.
Parameters
  • \Joomla\CMS\Toolbar\Toolbar $toolbar The Toolbar instance to attach to the button
  • string $type Button Type
Returns
  • \Joomla\CMS\Toolbar\ToolbarButton
Since
  • 3.8.0
-
  • \InvalidArgumentException

Method createButton - Source code

/**
 * Creates a new toolbar button.
 *
 * @param   Toolbar  $toolbar  The Toolbar instance to attach to the button
 * @param   string   $type     Button Type
 *
 * @return  ToolbarButton
 *
 * @since   3.8.0
 * @throws  \InvalidArgumentException
 */
public function createButton(Toolbar $toolbar, string $type) : ToolbarButton
{
    $normalisedType = ucfirst($type);
    $buttonClass = $this->loadButtonClass($normalisedType);
    if (!$buttonClass) {
        $dirs = $toolbar->getButtonPath();
        $file = InputFilter::getInstance()->clean(str_replace('_', DIRECTORY_SEPARATOR, strtolower($type)) . '.php', 'path');
        if ($buttonFile = Path::find($dirs, $file)) {
            include_once $buttonFile;
        } else {
            Log::add(Text::sprintf('JLIB_HTML_BUTTON_NO_LOAD', $buttonClass, $buttonFile), Log::WARNING, 'jerror');
            throw new \InvalidArgumentException(Text::sprintf('JLIB_HTML_BUTTON_NO_LOAD', $buttonClass, $buttonFile));
        }
    }
    if (!class_exists($buttonClass)) {
        throw new \InvalidArgumentException(sprintf('Class `%1$s` does not exist, could not create a toolbar button.', $buttonClass));
    }
    // Check for a possible service from the container otherwise manually instantiate the class
    if ($this->getContainer()->has($buttonClass)) {
        return $this->getContainer()->get($buttonClass);
    }
    /** @var ToolbarButton $button */
    $button = new $buttonClass($normalisedType);
    return $button->setParent($toolbar);
}