Back to LegacyModelLoaderTrait class

Method createModelFromComponent

private static \Joomla\CMS\MVC\Model\ModelInterface|null
createModelFromComponent
(mixed $type, mixed $prefix = '', mixed $config = [])
Returns a Model object by loading the component from the prefix.
Parameters
  • string $type The model type to instantiate
  • string $prefix Prefix for the model class name. Optional.
  • array $config Configuration array for model. Optional.
Returns
  • \Joomla\CMS\MVC\Model\ModelInterface|null A ModelInterface instance or null on failure
Since
  • 4.0.0
Deprecated
  • 5.0

Method createModelFromComponent - Source code

/**
 * Returns a Model object by loading the component from the prefix.
 *
 * @param   string  $type    The model type to instantiate
 * @param   string  $prefix  Prefix for the model class name. Optional.
 * @param   array   $config  Configuration array for model. Optional.
 *
 * @return  ModelInterface|null   A ModelInterface instance or null on failure
 *
 * @since       4.0.0
 * @deprecated  5.0 See getInstance
 */
private static function createModelFromComponent($type, $prefix = '', $config = []) : ?ModelInterface
{
    // Do nothing when prefix is not given
    if (!$prefix) {
        return null;
    }
    // Boot the component
    $componentName = 'com_' . str_replace('model', '', strtolower($prefix));
    $component = Factory::getApplication()->bootComponent($componentName);
    // When it is a legacy component or not a MVCFactoryService then ignore
    if ($component instanceof LegacyComponent || !$component instanceof MVCFactoryServiceInterface) {
        return null;
    }
    // Setup the client
    $client = Factory::getApplication()->getName();
    // Detect the client based on the include paths
    $adminPath = Path::clean(JPATH_ADMINISTRATOR . '/components/' . $componentName);
    $sitePath = Path::clean(JPATH_SITE . '/components/' . $componentName);
    foreach (self::addIncludePath() as $path) {
        if (strpos($path, $adminPath) !== false) {
            $client = 'Administrator';
            break;
        }
        if (strpos($path, $sitePath) !== false) {
            $client = 'Site';
            break;
        }
    }
    // Create the model
    $model = $component->getMVCFactory()->createModel($type, $client, $config);
    // When the model can't be loaded, then return null
    if (!$model) {
        return null;
    }
    // Return the model instance
    return $model;
}